/// <summary> /// Post data to a HTTP resource and return the response. /// </summary> /// <param name="request">A HttpWebRequest object.</param> /// <param name="serializer">A FormValuesSerializer object to serialize data to HTTP request stream.</param> /// <param name="data">The data to post.</param> /// <param name="name">If data is not complex type, use to name it.</param> /// <returns></returns> public static HttpWebResponse PostRequest(HttpWebRequest request, FormValuesSerializer serializer, object data, string name) { if (request == null) { throw new ArgumentNullException("request"); } string boundary = null; serializer = serializer ?? new FormValuesSerializer(); bool needEncode = FormValuesSerializer.CheckEncodeRequirement(data); request.Method = WebRequestMethods.Http.Post; if (needEncode) { request.ContentType = "application/x-www-form-urlencoded"; } else { request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary = GetRandomBoundary()); } if (data != null) { using (Stream writer = request.GetRequestStream()) { if (needEncode) { serializer.Serialize(data, name, writer); } else { serializer.Serialize(data, name, boundary, writer); } } } else { //avoid sending 411 server error request.ContentLength = 0; } return((HttpWebResponse)request.GetResponse()); }
/// <summary> /// Begins a async operation to post data to a HTTP resource. /// </summary> /// <param name="request">A HttpWebRequest object.</param> /// <param name="serializer">A FormValuesSerializer object to serialize data to HTTP request stream.</param> /// <param name="data">The data to post.</param> /// <param name="name">If data is not complex type, use to name it.</param> /// <param name="callback">The callback mehtod to invoke when async operation has completed.</param> /// <param name="userState">A user specified data.</param> /// <returns></returns> /// <remarks>If you want to upload a file, <paramref name="data"/> must has a member of HttpPostingFile type.</remarks> public static IAsyncResult BeginPostRequest(HttpWebRequest request, FormValuesSerializer serializer, object data, string name, AsyncCallback callback, object userState) { if (request == null) { throw new ArgumentNullException("request"); } string boundary = null; serializer = serializer ?? new FormValuesSerializer(); bool needEncode = FormValuesSerializer.CheckEncodeRequirement(data); AsyncResult <HttpWebResponse> result = new AsyncResult <HttpWebResponse>(callback, userState); request.Method = WebRequestMethods.Http.Post; if (needEncode) { request.ContentType = "application/x-www-form-urlencoded"; } else { request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary = GetRandomBoundary()); } if (data != null) { NetworkRequestAsyncTimeout.RegisterRequest(request.BeginGetRequestStream((streamResult) => { HttpWebRequest httpRequest = (HttpWebRequest)streamResult.AsyncState; try { using (Stream writer = httpRequest.EndGetRequestStream(streamResult)) { if (needEncode) { serializer.Serialize(data, name, writer); } else { serializer.Serialize(data, name, boundary, writer); } } NetworkRequestAsyncTimeout.RegisterRequest(httpRequest.BeginGetResponse((responseResult) => { Exception exception = null; HttpWebResponse response = null; try { response = (HttpWebResponse)((HttpWebRequest)responseResult.AsyncState).EndGetResponse(responseResult); } catch (Exception ex) { exception = ex; } result.MarkCompleted(exception, response); }, httpRequest), httpRequest, httpRequest.Timeout); } catch (WebException ex) { result.MarkCompleted(ex, (HttpWebResponse)ex.Response); } catch (Exception ex) { result.MarkCompleted(ex); } }, request), request, request.Timeout); } else { //avoid sending 411 server error request.ContentLength = 0; NetworkRequestAsyncTimeout.RegisterRequest(request.BeginGetResponse((responseResult) => { Exception exception = null; HttpWebResponse response = null; try { response = (HttpWebResponse)((HttpWebRequest)responseResult.AsyncState).EndGetResponse(responseResult); } catch (Exception ex) { exception = ex; } result.MarkCompleted(exception, response); }, request), request, request.Timeout); } return(result); }