public FormNameValuePair(FormValuesSerializer serializer, string name, IFormValue value, string contentType, IDictionary <string, string> parameters)
 {
     this.m_name        = name;
     this.m_value       = value;
     this.m_contentType = contentType;
     this.m_serializer  = serializer;
     this.m_parameters  = new Dictionary <string, string> {
         { "name", name },
     };
     if (parameters != null)
     {
         foreach (KeyValuePair <string, string> item in parameters)
         {
             this.m_parameters[item.Key] = item.Value;
         }
     }
 }
Exemplo n.º 2
0
        /// <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());
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
 /// <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="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, AsyncCallback callback, object userState)
 {
     return(BeginPostRequest(request, serializer, data, null, callback, userState));
 }
Exemplo n.º 5
0
 /// <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>
 /// <returns></returns>
 public static HttpWebResponse PostRequest(HttpWebRequest request, FormValuesSerializer serializer, object data)
 {
     return(PostRequest(request, serializer, data, null));
 }
 public FileContentFormValue(FormValuesSerializer serializer, HttpPostingFile file)
 {
     this.m_file       = file;
     this.m_serializer = serializer;
 }
 public StringFormValue(FormValuesSerializer serializer, string value)
 {
     this.m_value      = value;
     this.m_serializer = serializer;
 }
 public FormNameValuePair(FormValuesSerializer serializer, string name, IFormValue value, string contentType)
     : this(serializer, name, value, contentType, null)
 {
 }