c# - How to add parameters into a WebRequest? -


I need a method to call a webservice, so I wrote this code:

  private string urlPath = "http://xxx.xxx.xxx/manager/"; String request = urlPath + "index.php / org / get_org_form"; WebRequest webRequest = WebRequest.Create (requested); WebRequest.method = "post"; WebRequest.ContentType = "app / x-www-form-url expired"; WebRequest WebRequest.ContentLength = 0; WebResponse webResponse = webRequest.GetResponse ();  

But this method requires some parameters such as the following:

POST data:

  _username: 'API user', // API authentication username _password: 'API password', // API authentication password  

I can add these parameters in this Webrequest

Thanks in advance.

If these parameters url-string to them? 'And' & amp; For example, the characters

If these are the criteria of POST request, then you have to write it to create postal data and ask for the stream. Here is a sample method:

  private static string doRequestWithBytesPostData (string requestUri, String method, byte [] POSTDATA, CookieContainer cookieContainer, string userAgent, string acceptHeaderString, referenced string, string ContentType, ResponseUri out string) {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 = Cash Policy; request. Estimated = zero; If (! String.IsNullOrEmpty (method)) request.Method = method; If (! String.IsNullOrEmpty (acceptHeaderString)) request.Accept = acceptHeaderString; If (! String.isnalautoaccess (referrer)) request Referrer = Refresher; If (! String.IsNullOrEmpty (contentType)) request.ContentType = contentType; If (! String.IsNullOrEmpty (userAgent)) request.UserAgent = userAgent; If (Cookie Container! = Null) request Cook Container = Cookie Container; request. Timeout = stable. Request TimeOut; If (request.Method == "POST") {if (postData! = Null) {request.ContentLength = postData.Length; (Var datastream = request.GetRequestStream ()) using {datastream.Write (PostData, 0, PostDataLange); }}} (On the HttpWebResponse = request.GetResponse (as HttpWebResponse)) {{HttpWebResponse = Null!} {ResponseUri = httpWebResponse.ResponseUri.AbsoluteUri; CookieContainer.Add (httpWebResponse.Cookies); (Var Streamreader = New Stream Reader (using httpWebResponse.GetResponsestream ()) {Results = streamReader.ReadToEnd (); } Return results; }}}} Antivirus = null; Return tap; }  

Comments