/// <summary> /// get info from remote server /// </summary> /// <param name="pUrl"></param> /// <param name="pHeaders"></param> /// <param name="pCompletionHandler"></param> public void get(string pUrl, Dictionary <string, string> pHeaders, 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.AllowAutoRedirect = false; vWebReq.Method = "GET"; vWebReq.UserAgent = this.getUserAgent(); if (pHeaders != null) { foreach (KeyValuePair <string, string> kvp in pHeaders) { if (!kvp.Key.Equals("Content-Type")) { vWebReq.Headers.Add(kvp.Key, kvp.Value); } } } //fire request vWebResp = (HttpWebResponse)vWebReq.GetResponse(); handleWebResponse(vWebResp, pCompletionHandler); } catch (WebException wexp) { // 在 HTTP-400 错误情形下HttpWebResponse(上述vWebResp)已经为null值 // 需要在此处WebException的wexp.Response中获取真实的Response内容 // 2016-08-10, 18:20 FIXED by fengyh (http://fengyh.cn/) if (wexp.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse xWebResp = wexp.Response as HttpWebResponse; handleErrorWebResponse(xWebResp, pCompletionHandler, wexp); } } catch (Exception exp) { handleErrorWebResponse(vWebResp, pCompletionHandler, exp); } }
/// <summary> /// get info from remote server /// </summary> /// <param name="pUrl"></param> /// <param name="pHeaders"></param> /// <param name="pCompletionHandler"></param> public void get(string pUrl, Dictionary <string, string> pHeaders, 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.AllowAutoRedirect = false; vWebReq.Method = "GET"; vWebReq.UserAgent = this.getUserAgent(); if (pHeaders != null) { foreach (KeyValuePair <string, string> kvp in pHeaders) { if (!kvp.Key.Equals("Content-Type")) { vWebReq.Headers.Add(kvp.Key, kvp.Value); } } } //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); } }
/// <summary> /// post multi-part data form to remote server /// used to upload file /// </summary> /// <param name="pUrl"></param> /// <param name="pHeaders"></param> /// <param name="pPostParams"></param> /// <param name="httpFormFile"></param> /// <param name="pProgressHandler"></param> /// <param name="pCompletionHandler"></param> public void postMultipartDataForm(string pUrl, Dictionary <string, string> pHeaders, Dictionary <string, string> pPostParams, HttpFormFile pFormFile, ProgressHandler pProgressHandler, CompletionHandler pCompletionHandler) { if (pFormFile == null) { if (pCompletionHandler != null) { pCompletionHandler(ResponseInfo.fileError(new Exception("no file specified")), ""); } return; } HttpWebRequest vWebReq = null; HttpWebResponse vWebResp = null; try { vWebReq = (HttpWebRequest)WebRequest.Create(pUrl); vWebReq.ServicePoint.Expect100Continue = false; } catch (Exception ex) { if (pCompletionHandler != null) { pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), ""); } return; } try { vWebReq.UserAgent = this.getUserAgent(); vWebReq.AllowAutoRedirect = false; vWebReq.Method = "POST"; //create boundary string formBoundaryStr = this.createFormDataBoundary(); string contentType = string.Format("multipart/form-data; boundary={0}", formBoundaryStr); vWebReq.ContentType = contentType; if (pHeaders != null) { foreach (KeyValuePair <string, string> kvp in pHeaders) { if (!kvp.Key.Equals("Content-Type")) { vWebReq.Headers.Add(kvp.Key, kvp.Value); } } } //write post body vWebReq.AllowWriteStreamBuffering = true; byte[] formBoundaryBytes = Encoding.UTF8.GetBytes(string.Format("{0}{1}\r\n", FORM_BOUNDARY_TAG, formBoundaryStr)); byte[] formBoundaryEndBytes = Encoding.UTF8.GetBytes(string.Format("\r\n{0}{1}{2}\r\n", FORM_BOUNDARY_TAG, formBoundaryStr, FORM_BOUNDARY_TAG)); using (Stream vWebReqStream = vWebReq.GetRequestStream()) { //write params if (pPostParams != null) { foreach (KeyValuePair <string, string> kvp in pPostParams) { vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length); byte[] formPartTitleData = Encoding.UTF8.GetBytes( string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n", kvp.Key)); vWebReqStream.Write(formPartTitleData, 0, formPartTitleData.Length); byte[] formPartBodyData = Encoding.UTF8.GetBytes(string.Format("\r\n{0}\r\n", kvp.Value)); vWebReqStream.Write(formPartBodyData, 0, formPartBodyData.Length); } } vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length); //write file name string filename = pFormFile.Filename; if (string.IsNullOrEmpty(filename)) { filename = this.createRandomFilename(); } byte[] filePartTitleData = Encoding.UTF8.GetBytes( string.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", filename)); vWebReqStream.Write(filePartTitleData, 0, filePartTitleData.Length); //write content type string mimeType = FORM_MIME_OCTECT; //!!!注意这里 @fengyh 2016-08-17 15:00 if (!string.IsNullOrEmpty(pFormFile.ContentType)) { mimeType = pFormFile.ContentType; } byte[] filePartMimeData = Encoding.UTF8.GetBytes(string.Format("Content-Type: {0}\r\n\r\n", mimeType)); vWebReqStream.Write(filePartMimeData, 0, filePartMimeData.Length); //write file data switch (pFormFile.BodyType) { case HttpFileType.FILE_PATH: try { FileStream fs = File.Open(pFormFile.BodyFile, FileMode.Open, FileAccess.Read); this.writeHttpRequestBody(fs, vWebReqStream); } catch (Exception fex) { if (pCompletionHandler != null) { pCompletionHandler(ResponseInfo.fileError(fex), ""); } } break; case HttpFileType.FILE_STREAM: this.writeHttpRequestBody(pFormFile.BodyStream, vWebReqStream); break; case HttpFileType.DATA_BYTES: vWebReqStream.Write(pFormFile.BodyBytes, 0, pFormFile.BodyBytes.Length); break; case HttpFileType.DATA_SLICE: vWebReqStream.Write(pFormFile.BodyBytes, pFormFile.Offset, pFormFile.Count); break; } vWebReqStream.Write(formBoundaryEndBytes, 0, formBoundaryEndBytes.Length); 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); } }
/// <summary> /// post binary data to remote server /// </summary> /// <param name="pUrl"></param> /// <param name="pHeaders"></param> /// <param name="pPostData"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <param name="pCompletionHandler"></param> 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); } }
/// <summary> /// post the url encoded form to remote server /// </summary> /// <param name="pUrl"></param> /// <param name="pHeaders"></param> /// <param name="pParamDict"></param> /// <param name="pCompletionHandler"></param> public void postForm(string pUrl, Dictionary <string, string> pHeaders, Dictionary <string, string[]> pPostParams, 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"; vWebReq.ContentType = FORM_MIME_URLENCODED; if (pHeaders != null) { foreach (KeyValuePair <string, string> kvp in pHeaders) { if (!kvp.Key.Equals("Content-Type")) { vWebReq.Headers.Add(kvp.Key, kvp.Value); } } } // format the post body StringBuilder vPostBody = new StringBuilder(); if (pPostParams != null) { foreach (KeyValuePair <string, string[]> kvp in pPostParams) { foreach (string vVal in kvp.Value) { vPostBody.AppendFormat("{0}={1}&", Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(vVal)); } } // write data vWebReq.AllowWriteStreamBuffering = true; using (Stream vWebReqStream = vWebReq.GetRequestStream()) { vWebReqStream.Write(Encoding.UTF8.GetBytes(vPostBody.ToString()), 0, vPostBody.Length - 1); 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); } }