2010-07-19 123 views
36

我需要调用从Web服务的方法,所以我写了这个代码:如何将参数添加到WebRequest中?

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(); 

但这种方法需要一些参数,如下:

发表数据:

_username:'API USER',   // api authentication username 

_password:'API PASSWORD',  // api authentication password 

如何将这些参数添加到此Webrequest中?

在此先感谢。

回答

29

如果这些是url-string的参数,那么你需要通过'?'来添加它们。和'&'字符,例如http://example.com/index.aspx?username=Api_user&password=Api_password

如果这些是POST请求的参数,那么您需要创建POST数据并将其写入请求流。这里是样品的方法:

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; 
     } 
+0

@Omid一个数字,表示系统应如何长时间后放弃等待。 – RyanfaeScotland 2015-02-13 11:32:43

+1

与@ CoderHawk的回答相比似乎有些复杂。 – Kehlan 2015-05-28 18:44:39

+0

老话题但是..什么是“常量”? – 2016-08-02 23:16:55

68

使用流写入内容的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(); 
+0

你给这个'newStream.Write(data,0,data.Length)添加了多少额外参数' – Smith 2011-12-24 20:14:55

+0

@Smith - 用户名和密码,请看第一行 – CoderHawk 2011-12-27 05:27:48

+0

nice @CoderHawk ..谢谢最佳编码实践 – KingRider 2016-08-11 15:50:46

8

对于做FORM职位,最好的办法是使用WebClient.UploadValues()用POST方法。

4

希望这个作品

webRequest.Credentials= new NetworkCredential("API_User","API_Password");