1

web服務發佈方法對我來說是新的。你能幫我解決我做錯了什麼嗎?Webclient中的發佈方法

WebClient webClient = new WebClient(); 

webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 

       webClient.Encoding = Encoding.UTF8; 
       webClient.UploadStringCompleted += new UploadStringCompletedEventHandler((sender, e) => 
       { 
        if (e.Error != null) 
        { 
         return; 
        } 
        string result = e.Result; 
       }); 
       string uri = "http://localhost:60696/service/getlogin/"; 
       StringBuilder postData = new StringBuilder(); 
       postData.AppendFormat("/{0}/{1}", "username", HttpUtility.UrlEncode(textBox1.Text)); 
       postData.AppendFormat("/{0}/{1}", "password", HttpUtility.UrlEncode(textBox2.Text)); 
       webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString(); 
       webClient.UploadStringAsync(new Uri(uri, UriKind.RelativeOrAbsolute),"POST", postData.ToString()); 

順便說一下,如果我與參數訪問URL和價值附加其做工精細

這樣("http://localhost:60696/service/getlogin/username,123,password,456").

回答

2

的POSTDATA似乎編碼錯誤,應該是這樣的:

postData.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(textBox1.Text)); 
postData.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(textBox2.Text)); 
+0

thx它真的有幫助。 – BharathNadadur

+0

如果您需要我的建議,只需使用RestSharp而不是手動提出請求,它將爲您節省大量時間和頭痛! –

+0

Restsharp?...我以前沒有用過它。你可以給我這是什麼以及如何使用它的參考 – BharathNadadur