Http (GET/POST)请求
1 /// <summary> 2 /// Http (GET/POST) 3 /// </summary> 4 /// <param name="url">请求URL</param> 5 /// <param name="parameters">请求参数</param> 6 /// <param name="method">请求方法</param> 7 /// <returns>响应内容</returns> 8 static string sendPost(string url, IDictionary<string, string> parameters, string method) 9 { 10 if (method.ToLower() == "post") 11 { 12 HttpWebRequest req = null; 13 HttpWebResponse rsp = null; 14 System.IO.Stream reqStream = null; 15 try 16 { 17 req = (HttpWebRequest)WebRequest.Create(url); 18 req.Method = method; 19 req.KeepAlive = false; 20 req.ProtocolVersion = HttpVersion.Version10; 21 req.Timeout = 5000; 22 req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; 23 byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8")); 24 reqStream = req.GetRequestStream(); 25 reqStream.Write(postData, 0, postData.Length); 26 rsp = (HttpWebResponse)req.GetResponse(); 27 Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet); 28 return GetResponseAsString(rsp, encoding); 29 } 30 catch (Exception ex) 31 { 32 return ex.Message; 33 } 34 finally 35 { 36 if (reqStream != null) reqStream.Close(); 37 if (rsp != null) rsp.Close(); 38 } 39 } 40 else 41 { 42 //创建请求 43 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8")); 44 45 //GET请求 46 request.Method = "GET"; 47 request.ReadWriteTimeout = 5000; 48 request.ContentType = "text/html;charset=UTF-8"; 49 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 50 Stream myResponseStream = response.GetResponseStream(); 51 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); 52 53 //返回内容 54 string retString = myStreamReader.ReadToEnd(); 55 return retString; 56 } 57 }
1 /// <summary> 2 /// 组装普通文本请求参数。 3 /// </summary> 4 /// <param name="parameters">Key-Value形式请求参数字典</param> 5 /// <returns>URL编码后的请求数据</returns> 6 static string BuildQuery(IDictionary<string, string> parameters, string encode) 7 { 8 StringBuilder postData = new StringBuilder(); 9 bool hasParam = false; 10 IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator(); 11 while (dem.MoveNext()) 12 { 13 string name = dem.Current.Key; 14 string value = dem.Current.Value; 15 // 忽略参数名或参数值为空的参数 16 if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value) 17 { 18 if (hasParam) 19 { 20 postData.Append("&"); 21 } 22 postData.Append(name); 23 postData.Append("="); 24 if (encode == "gb2312") 25 { 26 postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312"))); 27 } 28 else if (encode == "utf8") 29 { 30 postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8)); 31 } 32 else 33 { 34 postData.Append(value); 35 } 36 hasParam = true; 37 } 38 } 39 return postData.ToString(); 40 }
1 /// <summary> 2 /// 把响应流转换为文本。 3 /// </summary> 4 /// <param name="rsp">响应流对象</param> 5 /// <param name="encoding">编码方式</param> 6 /// <returns>响应文本</returns> 7 static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding) 8 { 9 System.IO.Stream stream = null; 10 StreamReader reader = null; 11 try 12 { 13 // 以字符流的方式读取HTTP响应 14 stream = rsp.GetResponseStream(); 15 reader = new StreamReader(stream, encoding); 16 return reader.ReadToEnd(); 17 } 18 finally 19 { 20 // 释放资源 21 if (reader != null) reader.Close(); 22 if (stream != null) stream.Close(); 23 if (rsp != null) rsp.Close(); 24 } 25 }
各自努力