Qiniu.Http.HttpManager.postData C# (CSharp) Method

postData() public method

post binary data to remote server
public postData ( string pUrl, string>.Dictionary pHeaders, byte pPostData, int offset, int count, string contentType, CompletionHandler pCompletionHandler ) : void
pUrl string
pHeaders string>.Dictionary
pPostData byte
offset int
count int
contentType string
pCompletionHandler CompletionHandler
return void
        public void postData(string pUrl, Dictionary<string, string> pHeaders,
            byte[] pPostData, int offset, int count, string contentType,
            CompletionHandler pCompletionHandler)
        {
            HttpWebRequest vWebReq = null;
            HttpWebResponse vWebResp = null;
            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method = "POST";
                if (!string.IsNullOrEmpty(contentType))
                {
                    vWebReq.ContentType = contentType;
                }
                else
                {
                    vWebReq.ContentType = FORM_MIME_OCTECT;
                }
                if (pHeaders != null)
                {
                    foreach (KeyValuePair<string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                vWebReq.AllowWriteStreamBuffering = true;
                // write data
                using (Stream vWebReqStream = vWebReq.GetRequestStream())
                {
                    vWebReqStream.Write(pPostData, offset, count);
                    vWebReqStream.Flush();
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
            finally
            {
                if (vWebResp != null)
                {
                    vWebResp.Close();
                    vWebResp = null;
                }

                if (vWebReq != null)
                {
                    vWebReq.Abort();
                    vWebReq = null;
                }
            }
        }

Same methods

HttpManager::postData ( string pUrl, string>.Dictionary pHeaders, byte pPostData, string contentType, CompletionHandler pCompletionHandler ) : void

Usage Example

Esempio n. 1
0
 public HttpResult batch(string ops)
 {
     HttpResult batchResult = null;
     string url = string.Format("{0}{1}", Config.RS_HOST, "/batch");
     string token = Auth.createManageToken(url, Encoding.UTF8.GetBytes(ops), this.mac);
     HttpManager httpManager = new HttpManager();
     httpManager.setAuthHeader(token);
     httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
     {
         batchResult = new FetchResult();
         batchResult.Response = response;
         batchResult.ResponseInfo = respInfo;
     });
     PostArgs postArgs = new PostArgs();
     postArgs.Data = Encoding.UTF8.GetBytes(ops);
     httpManager.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
     httpManager.PostArgs = postArgs;
     httpManager.postData(url);
     return batchResult;
 }