Passing Paramters

Topics: Web Api
Oct 4, 2011 at 12:01 AM

I created a WCF RESTFUL service and it runs just fine - this will NOT be hosted on the same application. I plan to call it through HTTP Client. Now I know that the HTTP Client underwent some drastic changes so I bit the bullet and played around with it. I'm liking it but I struggled with passing of parameters:

 

var logoninfo = new Logon // pass string an xml string from a form collection
            {
                username = username,
                password = password
            };

            
            ObjectContent o = new ObjectContent(logoninfo);

            using (HttpClient client = new HttpClient())
            {


                var request = new HttpRequestMessage()
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(baseaddress + "/MainService/ValidateUser"),
                    Content = new ObjectContent(logoninfo,"application/xml")
                                        
                };

                                                               
                var response = client.Send(request);
                                
                //memberallowed = (new StreamReader(response.Content.ContentReadStream));

                

            }
            

The problem I think lies with Content - I'm just trying to pass two parameters username and password that match the URI on the WCF Restful. Any tips on this? Before attemping to pass any parameters, the WCF call is succseful with 200 in response but it generates an error when attempting to pass parameters. The error reads:

Cannot send a content-body with this verb-type


I suspect I'm supposed to serialize it in the way to match the WCF which is basically:

 [OperationContract]
        [WebGet(UriTemplate = "ValidateUser?username={username}&password={password}")]
        public Stream Validateuser(string username, string password)
        {

 [OperationContract]
        [WebGet(UriTemplate = "ValidateUser?username={username}&password={password}")]
        public Stream Validateuser(string username, string password)
        {

Thanks,

Glenn (NOT the Glenn who re-invented the HTTP wheel!) :-)


Coordinator
Oct 4, 2011 at 8:45 PM

Based on your URI template you actually need to specify the username and password as query string parameters in the URI. You do this by building up the appropriate RequestURI for your request. Something like this:

                    RequestUri = new Uri(baseaddress + "/MainService/ValidateUser?username=" + username + "&password=" + password)
 

ObjectContent is actually used to specify the content of the request body.

Please note however that passing passwords in the URI is a shady business as anyone will be able to see the users credentials. Instead you should consider using a standard authentication protocol, like Basic over SSL, or OAuth.

Daniel Ro

From: devling [email removed]
Sent: Monday, October 03, 2011 5:02 PM
To: Daniel Roth
Subject: Passing Paramters [wcf:274648]

From: devling

I created a WCF RESTFUL service and it runs just fine - this will NOT be hosted on the same application. I plan to call it through HTTP Client. Now I know that the HTTP Client underwent some drastic changes so I bit the bullet and played around with it. I'm liking it but I struggled with passing of parameters:

var logoninfo = new Logon // pass string an xml string from a form collection
            {
                username = username,
                password = password
            };
 
            
            ObjectContent o = new ObjectContent(logoninfo);
 
            using (HttpClient client = new HttpClient())
            {
 
 
                var request = new HttpRequestMessage()
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(baseaddress + "/MainService/ValidateUser"),
                    Content = new ObjectContent(logoninfo,"application/xml")
                                        
                };
 
                                                               
                var response = client.Send(request);
                                
                //memberallowed = (new StreamReader(response.Content.ContentReadStream));
 
                
 
            }
            

The problem I think lies with Content - I'm just trying to pass two parameters username and password that match the URI on the WCF Restful. Any tips on this? Before attemping to pass any parameters, the WCF call is succseful with 200 in response but it generates an error when attempting to pass parameters. The error reads:

Cannot send a content-body with this verb-type


I suspect I'm supposed to serialize it in the way to match the WCF which is basically:

[OperationContract]
[WebGet(UriTemplate = "ValidateUser?username={username}&password={password}")]
public Stream Validateuser(string username, string password)
{

 [OperationContract]
        [WebGet(UriTemplate = "ValidateUser?username={username}&password={password}")]
        public Stream Validateuser(string username, string password)
        {

Thanks,

Glenn (NOT the Glenn who re-invented the HTTP wheel!) :-)