12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- namespace WeiXin.Library.Utility
- {
- class HttpUtility
- {
- /// <summary>
- /// 发送请求
- /// </summary>
- /// <param name="url">Url地址</param>
- /// <param name="data">数据</param>
- public static string SendHttpRequest(string url, string data)
- {
- return SendPostHttpRequest(url, "application/x-www-form-urlencoded", data);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string GetData(string url)
- {
- return SendGetHttpRequest(url, "application/x-www-form-urlencoded");
- }
- /// <summary>
- /// 发送请求
- /// </summary>
- /// <param name="url">Url地址</param>
- /// <param name="method">方法(post或get)</param>
- /// <param name="method">数据类型</param>
- /// <param name="requestData">数据</param>
- public static string SendPostHttpRequest(string url, string contentType, string requestData)
- {
- WebRequest request = (WebRequest)HttpWebRequest.Create(url);
- request.Method = "POST";
- byte[] postBytes = null;
- request.ContentType = contentType;
- postBytes = Encoding.UTF8.GetBytes(requestData);
- request.ContentLength = postBytes.Length;
- using (Stream outstream = request.GetRequestStream())
- {
- outstream.Write(postBytes, 0, postBytes.Length);
- }
- string result = string.Empty;
- using (WebResponse response = request.GetResponse())
- {
- if (response != null)
- {
- using (Stream stream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
- {
- result = reader.ReadToEnd();
- }
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 发送请求
- /// </summary>
- /// <param name="url">Url地址</param>
- /// <param name="method">方法(post或get)</param>
- /// <param name="method">数据类型</param>
- /// <param name="requestData">数据</param>
- public static string SendGetHttpRequest(string url, string contentType)
- {
- WebRequest request = (WebRequest)HttpWebRequest.Create(url);
- request.Method = "GET";
- request.ContentType = contentType;
- string result = string.Empty;
- using (WebResponse response = request.GetResponse())
- {
- if (response != null)
- {
- using (Stream stream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
- {
- result = reader.ReadToEnd();
- }
- }
- }
- }
- return result;
- }
- }
- }
|