public static StringBuilder HttpPost(string Url, byte[] Postdata, string i) { StringBuilder content = new StringBuilder(); try { WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] bycontent = wc.UploadData(Url, "POST", Postdata); string strcontent = System.Text.Encoding.UTF8.GetString(bycontent); //strcontent=strcontent.Replace(""", "“"); content.Append(strcontent); } catch (Exception ex) { throw new ServiceCustomException("HTTP请求失败!", ex); } return content; }
使用:StringBuilder str = NetUtil.HttpPost(posturl, byte1, "1");
// StringBuilder sbd = NetUtil.Request(geturl, HttpMethod.GET);//此方式为Get实例
/// <summary> /// 发出Http请求,并返回服务器的响应 /// </summary> /// <param name="url">请求的网址</param> /// <param name="dataToPost">post的数据</param> /// <returns>服务器的响应</returns> public static StringBuilder Request(string url, HttpMethod method, WebHeaderCollection headers = null, string accept = null, string contentType = null, Encoding encoding = null, bool keepAlive = false, int timeout = 1000 * 600, int readWriteTimeout = 1000 * 600, int writeCount = 256, CookieContainer cookieContainer = null, byte[] dataToPost = null) { StringBuilder result = new StringBuilder(); HttpWebRequest request = null; HttpWebResponse response = null; Stream responseStream = null; StreamReader reader = null; try { request = (HttpWebRequest)WebRequest.Create(url); request.Method = method.ToString(); if (headers != null) request.Headers = headers; if (!string.IsNullOrEmpty(accept)) request.Accept = accept; if (!string.IsNullOrEmpty(contentType)) request.ContentType = contentType; request.KeepAlive = keepAlive; if (dataToPost != null && !dataToPost.Length.Equals(0)) { request.ContentLength = dataToPost.Length; } request.Timeout = timeout; request.ReadWriteTimeout = readWriteTimeout; if (encoding == null) encoding = Encoding.GetEncoding("utf-8"); if (method.Equals(HttpMethod.POST) && dataToPost != null && !dataToPost.Length.Equals(0)) { using (Stream requestStream = request.GetRequestStream()) { int index = 0; while (index < dataToPost.Length) { if (dataToPost.Length - index <= writeCount) { requestStream.Write(dataToPost, index, dataToPost.Length - index); index += dataToPost.Length - index; break; } else { requestStream.Write(dataToPost, index, writeCount); index += writeCount; } } } } if (cookieContainer == null) request.CookieContainer = new CookieContainer(); else request.CookieContainer = cookieContainer; response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); reader = new StreamReader(responseStream, encoding); string line = string.Empty; while ((line = reader.ReadLine()) != null) { result.Append(line); } reader.Close(); } catch (WebException ex) { throw ex; } catch (Exception ex) { } finally { if (responseStream != null) { responseStream.Close(); responseStream = null; } if (reader != null) { reader.Close(); reader = null; } if (response != null) { response.Close(); response = null; } } return result; }
使用: StringBuilder str = NetUtil.Request(posturl, HttpMethod.POST, null, null, "application/x-www-form-urlencoded", System.Text.Encoding.UTF8,
dataToPost: byte1);
/// <summary> /// 提交数据请求 方法一 /// </summary> /// <param name="POSTURL">请求提交的地址 如:http://xxx.xxx.xxx/interface/TestPostRequest</param> /// <param name="PostData">提交的数据(字符串)</param> /// <returns></returns> public static string RequestData(string POSTURL, string PostData) { //发送请求的数据 WebRequest myHttpWebRequest = WebRequest.Create(POSTURL); myHttpWebRequest.Method = "POST"; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(PostData); myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //myHttpWebRequest.ContentType = "application/json"; myHttpWebRequest.ContentLength = byte1.Length; Stream newStream = myHttpWebRequest.GetRequestStream(); newStream.Write(byte1, 0, byte1.Length); newStream.Close(); //发送成功后接收返回的XML信息 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); string lcHtml = string.Empty; Encoding enc = Encoding.GetEncoding("UTF-8"); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, enc); lcHtml = streamReader.ReadToEnd(); return lcHtml; }
使用:NetUtil.RequestData(posturl, json);
public static string BuildQuery(IDictionary<string, string> parameters) { StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; // 忽略参数名或参数值为空的参数 if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value)) { if (hasParam) { postData.Append("&"); } postData.Append(name); postData.Append("="); //postData.Append(UrlEncode(value)); postData.Append(value); hasParam = true; } } return postData.ToString(); }
使用:
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("canshu", "dddddddd);
string str1 = NetUtil.BuildQuery(dic); //dic为要post的所有参数
byte[] byte1 = System.Text.Encoding.UTF8.GetBytes(str1);
//posturl为传递的url参数
第三种方式中json为:string json = "canshu=" + "dddddddd";