Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add parameters into a WebRequest?

I need to call a method from a webservice, so I've written this code:

 private string urlPath = "http://xxx.xxx.xxx/manager/";  string request = urlPath + "index.php/org/get_org_form";  WebRequest webRequest = WebRequest.Create(request);  webRequest.Method = "POST";  webRequest.ContentType = "application/x-www-form-urlencoded";  webRequest.  webRequest.ContentLength = 0;  WebResponse webResponse = webRequest.GetResponse(); 

But this method requires some parameters, as following:

Post data:

_username:'API USER',         // api authentication username  _password:'API PASSWORD',     // api authentication password 

How can I add these parameters into this Webrequest?

Thanks in advance.

like image 771
Quan Mai Avatar asked Jul 19 '10 09:07

Quan Mai


People also ask

How to pass parameters to WebRequest c#?

Try to create request for authorization form, if the data is correct - you will be redirected to another page, if not true, will return an authorization form. WebRequest request = WebRequest. Create("http://www.request.com/"); request.

What is WebRequest C#?

The WebRequest is an abstract base class. So you actually don't use it directly. You use it through it derived classes - HttpWebRequest and FileWebRequest. You use Create method of WebRequest to create an instance of WebRequest. GetResponseStream returns data stream.


2 Answers

Use stream to write content to webrequest

string data = "username=<value>&password=<value>"; //replace <value> byte[] dataStream = Encoding.UTF8.GetBytes(data); private string urlPath = "http://xxx.xxx.xxx/manager/"; string request = urlPath + "index.php/org/get_org_form"; WebRequest webRequest = WebRequest.Create(request); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = dataStream.Length;   Stream newStream=webRequest.GetRequestStream(); // Send the data. newStream.Write(dataStream,0,dataStream.Length); newStream.Close(); WebResponse webResponse = webRequest.GetResponse();   
like image 190
CoderHawk Avatar answered Sep 18 '22 19:09

CoderHawk


If these are the parameters of url-string then you need to add them through '?' and '&' chars, for example http://example.com/index.aspx?username=Api_user&password=Api_password.

If these are the parameters of POST request, then you need to create POST data and write it to request stream. Here is sample method:

private static string doRequestWithBytesPostData(string requestUri, string method, byte[] postData,                                         CookieContainer cookieContainer,                                         string userAgent, string acceptHeaderString,                                         string referer,                                         string contentType, out string responseUri)         {             var result = "";             if (!string.IsNullOrEmpty(requestUri))             {                 var request = WebRequest.Create(requestUri) as HttpWebRequest;                 if (request != null)                 {                     request.KeepAlive = true;                     var cachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);                     request.CachePolicy = cachePolicy;                     request.Expect = null;                     if (!string.IsNullOrEmpty(method))                         request.Method = method;                     if (!string.IsNullOrEmpty(acceptHeaderString))                         request.Accept = acceptHeaderString;                     if (!string.IsNullOrEmpty(referer))                         request.Referer = referer;                     if (!string.IsNullOrEmpty(contentType))                         request.ContentType = contentType;                     if (!string.IsNullOrEmpty(userAgent))                         request.UserAgent = userAgent;                     if (cookieContainer != null)                         request.CookieContainer = cookieContainer;                      request.Timeout = Constants.RequestTimeOut;                      if (request.Method == "POST")                     {                         if (postData != null)                         {                             request.ContentLength = postData.Length;                             using (var dataStream = request.GetRequestStream())                             {                                 dataStream.Write(postData, 0, postData.Length);                             }                         }                     }                      using (var httpWebResponse = request.GetResponse() as HttpWebResponse)                     {                         if (httpWebResponse != null)                         {                             responseUri = httpWebResponse.ResponseUri.AbsoluteUri;                             cookieContainer.Add(httpWebResponse.Cookies);                             using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))                             {                                 result = streamReader.ReadToEnd();                             }                             return result;                         }                     }                 }             }             responseUri = null;             return null;         } 
like image 43
Eugene Cheverda Avatar answered Sep 17 '22 19:09

Eugene Cheverda