用WebClient来POST数据的问题
比如代码
client.UploadStringAsync(uri, "id=aaaaaaa");
其中client就是一个WebClient对象
问题是。。。。。在uri所指定的web页面,可以取到 id=evsegxvds 这整个字符串,但却不能以Request.Form["id"]的方式取得aaaaaaa
怎么解决这个问题?
[解决办法]
你这是get方式吧!
第二个参数直接传值吧!把ID写到第一个参数去试试!
[解决办法]
下面的代码是我读SLExtension看到的,应该对你有帮助:
/// <summary> /// Method extensions for SLExtension framework /// </summary> public static class SLExtension { .... #region WebClient /// <summary> /// Sends an HTML form. The request is sent using POST method /// </summary> /// <param name="webclient">The webclient.</param> /// <param name="uri">The URI of the resource to receive the file. </param> /// <param name="data">The form data to upload.</param> public static void SendHtmlForm(this WebClient webclient, Uri uri, IEnumerable<KeyValuePair<string, string>> data) { StringBuilder dataBuilder = new StringBuilder(); int cnt = 0; foreach (var item in data) { if (cnt > 0) { dataBuilder.Append('&'); } dataBuilder.Append(item.Key); dataBuilder.Append('='); dataBuilder.Append(HttpUtility.UrlEncode(item.Value)); } webclient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; webclient.UploadStringAsync(uri, dataBuilder.ToString()); } /// <summary> /// Sends an HTML form. The request is sent using POST method /// </summary> /// <param name="webclient">The webclient.</param> /// <param name="uri">The URI of the resource to receive the file. </param> /// <param name="data">The form data to upload.</param> /// <param name="userToken">user state</param> public static void SendHtmlForm(this WebClient webclient, Uri uri, IEnumerable<KeyValuePair<string, string>> data, object userToken) { StringBuilder dataBuilder = new StringBuilder(); int cnt = 0; foreach (var item in data) { if (cnt > 0) { dataBuilder.Append('&'); } dataBuilder.Append(item.Key); dataBuilder.Append('='); dataBuilder.Append(HttpUtility.UrlEncode(item.Value)); } webclient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; webclient.UploadStringAsync(uri, "POST", dataBuilder.ToString(), userToken); } #endregion WebClient #endregion Public Methods }
[解决办法]
WebClient.UploadStringAsync 方法 (Uri, String, String, Object)
參數
address
型別:System.Uri
要接收檔案之資源的 URI。 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。
method
型別:System.String
傳送檔案給資源所使用的 HTTP 方法。 如果為 null,則 HTTP 的預設值為 POST。
data
型別:System.String
要上載的字串。
userToken
型別:System.Object
使用者定義的物件,這個物件會在非同步作業完成時傳遞至叫用的方法。
[解决办法]