public static string DoService(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout) { HttpMethod method = paramers.Method; try { switch (method) { case HttpMethod.POST: return(DoPost(url, paramers, header, connectTimeout, readTimeout)); case HttpMethod.GET: return(DoGet(url, paramers, header, connectTimeout, readTimeout)); default: throw new Exception("不支持的HTTP方法类型"); } } catch (Exception e) { throw e; } throw new Exception("尝试访问服务器时异常!"); }
public static HttpParamers PostJsonParamers(string json) { HttpParamers paramers = new HttpParamers(HttpMethod.POST); paramers.JsonParamer = json; return(paramers); }
/** * 写入multipart参数 * * @param boundary * @param paramers * @param out * @throws IOException */ private static void WriteMutiContent(string boundary, HttpParamers paramers, ref Stream stream) { byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); // 写入文本请求参数 foreach (KeyValuePair <string, string> paramer in paramers.Paramers) { byte[] textBytes = GetTextEntry(paramer.Key, paramer.Value); stream.Write(boundaryBytes, 0, boundaryBytes.Length); stream.Write(textBytes, 0, textBytes.Length); } // 写入文件请求参数 if (paramers.FileStreams.Count > 0) { foreach (KeyValuePair <string, IFileItem> file in paramers.FileStreams) { IFileItem fileItem = file.Value; if (!fileItem.IsValid()) { throw new Exception("无效的文件流"); } byte[] fileBytes = GetFileEntry(file.Key, fileItem.GetFileName(), fileItem.GetMimeType()); stream.Write(boundaryBytes, 0, boundaryBytes.Length); stream.Write(fileBytes, 0, fileBytes.Length); fileItem.Write(ref stream); } } //写入批量文件请求 if (paramers.Files.Count > 0) { foreach (KeyValuePair <string, List <IFileItem> > listEntry in paramers.Files) { List <IFileItem> items = listEntry.Value; foreach (IFileItem item in items) { if (!item.IsValid()) { throw new Exception("无效的文件流"); } byte[] fileBytes = GetFileEntry(listEntry.Key, item.GetFileName(), item.GetMimeType()); stream.Write(boundaryBytes, 0, boundaryBytes.Length); stream.Write(fileBytes, 0, fileBytes.Length); item.Write(ref stream); } } } // 添加请求结束标志 byte[] endBoundary = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); stream.Write(endBoundary, 0, endBoundary.Length); }
public static void DoDownload(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout, ref Stream stream) { HttpWebRequest webRequest = null; try { string queryString = paramers.GetQueryString(); string getURL = BuildGetUrl(url, queryString); webRequest = HttpConnection.GetRequest(getURL, HttpMethod.GET, header); webRequest.ReadWriteTimeout = readTimeout; webRequest.Timeout = connectTimeout; GetResponseAsOutputStream(webRequest, ref stream); } catch (Exception e) { throw e; } }
public static string DoGet(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout) { HttpWebRequest webRequest = null; try { string queryString = paramers.GetQueryString(); string getURL = BuildGetUrl(url, queryString); webRequest = HttpConnection.GetRequest(getURL, HttpMethod.GET, header); webRequest.ReadWriteTimeout = readTimeout; webRequest.Timeout = connectTimeout; return(GetResponseAsString(webRequest)); } catch (Exception e) { throw e; } }
public static string DoPost(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout) { System.IO.Stream stream = null; HttpWebRequest webRequest = null; try { webRequest = HttpConnection.GetRequest(url, HttpMethod.POST, header); webRequest.ReadWriteTimeout = readTimeout; webRequest.Timeout = connectTimeout; if (paramers.IsMultipart()) { string boundary = "----sdkboundary" + StringUtils.Random(6); // 随机分隔线 webRequest.ContentType = CONTENT_MULTIPART + boundary; Stream memStream = new MemoryStream(); WriteMutiContent(boundary, paramers, ref memStream); webRequest.ContentLength = memStream.Length; Stream requestStream = webRequest.GetRequestStream(); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); } else if (paramers.IsJsonApplication()) { string jsonString = paramers.JsonParamer; byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonString); webRequest.ContentType = JSON_CONTENT_FORM; stream = webRequest.GetRequestStream(); stream.Write(jsonBytes, 0, jsonBytes.Length); stream.Close(); } else { string queryString = paramers.GetQueryString(); if (!String.IsNullOrEmpty(queryString)) { byte[] queryBytes = System.Text.Encoding.UTF8.GetBytes(queryString); webRequest.ContentType = CONTENT_FORM; stream = webRequest.GetRequestStream(); stream.Write(queryBytes, 0, queryBytes.Length); stream.Close(); } } return(GetResponseAsString(webRequest)); } catch (Exception e) { if (stream != null) { stream.Close(); } throw e; } }