Exemplo n.º 1
0
        public static string SendPostRequestWithHeaderAndBody(string apiUrl,
                                                              Dictionary <string, string> headerData, Dictionary <string, object> postData)//multipart/form-data
        {
            string strJsonData = "";

            try
            {
                HttpWebRequest request = WebRequest.Create(apiUrl) as HttpWebRequest;

                foreach (var item in headerData)
                {
                    request.Headers.Add(item.Key, item.Value);
                }

                request = FormUpload.SetPostWebRequestWithBody(request, postData);

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                strJsonData = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(strJsonData);
        }
Exemplo n.º 2
0
        public static string SendPostRequestWithBody(string postURL,
                                                     Dictionary <string, object> postParameters)//multipart/form-data
        {
            HttpWebResponse response = FormUpload.MultipartFormDataPost(postURL, "", postParameters);

            string strJsonData = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

            return(strJsonData);
        }
Exemplo n.º 3
0
        public static HttpWebRequest SetPostWebRequestWithBody(HttpWebRequest request, Dictionary <string, object> postParameters)
        {
            string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
            string contentType      = "multipart/form-data; boundary=" + formDataBoundary;

            byte[] formData = FormUpload.GetMultipartFormData(postParameters, formDataBoundary);
            if (request == null)
            {
                throw new NullReferenceException("request is not a http request");
            }

            request.Method          = "POST";
            request.ContentType     = contentType;
            request.CookieContainer = new CookieContainer();
            request.ContentLength   = formData.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(formData, 0, formData.Length);
                requestStream.Close();
            }
            return(request);
        }