/// <summary>
/// HTTP请求方式枚举
/// </summary>
public enum HttpMethod { POST, GET };
/// <summary>
/// http请求
/// </summary>
/// <param name="url"></param>
/// <param name="type">GET POST</param>
/// <param name="ht"></param>
/// <param name="encoder">编码默认UTF-8</param>
/// <returns></returns>
public static string HttpGet(string url, string type = "GET", Hashtable ht = null, Encoding encoder = null)
{
if (encoder == null) { encoder = Encoding.UTF8; }
string sendData = string.Empty;
if (ht != null && ht.Count > 0)
{
foreach (DictionaryEntry item in ht)
{
sendData += item.Key + "=" + item.Value + "&";
}
sendData = sendData.TrimEnd('&');
}
if (type.ToUpper() == "GET")
{
if (sendData.Length > 1)
url += (url.IndexOf("?") > -1 ? "&" : "?") + sendData;
}
string readStr = string.Empty;
try
{
HttpWebRequest http = (HttpWebRequest)WebRequest.Create(url);
http.Method = type;
http.Accept = "text/html, application/xhtml+xml, */*";
//Encoding encoder = Encoding.GetEncoding("UTF-8");
if (type.ToUpper() == "POST")
{
http.ContentType = "application/x-www-form-urlencoded";
if (sendData.Length > 1)
{
byte[] buffer = encoder.GetBytes(sendData);
http.ContentLength = buffer.Length;
http.GetRequestStream().Write(buffer, 0, buffer.Length);
}
}
Stream strame = http.GetResponse().GetResponseStream();
using (StreamReader read = new StreamReader(strame, encoder))
{
readStr = read.ReadToEnd();
}
}
catch (Exception ex)
{
readStr = "{\"msg\":\"HTTP错误,请检查访问接口!" + ex.Message + "\",\"stats\":-1,\"status\":-1}";
}
return readStr;
}
/// HTTP请求方式枚举
/// </summary>
public enum HttpMethod { POST, GET };
/// <summary>
/// http请求
/// </summary>
/// <param name="url"></param>
/// <param name="type">GET POST</param>
/// <param name="ht"></param>
/// <param name="encoder">编码默认UTF-8</param>
/// <returns></returns>
public static string HttpGet(string url, string type = "GET", Hashtable ht = null, Encoding encoder = null)
{
if (encoder == null) { encoder = Encoding.UTF8; }
string sendData = string.Empty;
if (ht != null && ht.Count > 0)
{
foreach (DictionaryEntry item in ht)
{
sendData += item.Key + "=" + item.Value + "&";
}
sendData = sendData.TrimEnd('&');
}
if (type.ToUpper() == "GET")
{
if (sendData.Length > 1)
url += (url.IndexOf("?") > -1 ? "&" : "?") + sendData;
}
string readStr = string.Empty;
try
{
HttpWebRequest http = (HttpWebRequest)WebRequest.Create(url);
http.Method = type;
http.Accept = "text/html, application/xhtml+xml, */*";
//Encoding encoder = Encoding.GetEncoding("UTF-8");
if (type.ToUpper() == "POST")
{
http.ContentType = "application/x-www-form-urlencoded";
if (sendData.Length > 1)
{
byte[] buffer = encoder.GetBytes(sendData);
http.ContentLength = buffer.Length;
http.GetRequestStream().Write(buffer, 0, buffer.Length);
}
}
Stream strame = http.GetResponse().GetResponseStream();
using (StreamReader read = new StreamReader(strame, encoder))
{
readStr = read.ReadToEnd();
}
}
catch (Exception ex)
{
readStr = "{\"msg\":\"HTTP错误,请检查访问接口!" + ex.Message + "\",\"stats\":-1,\"status\":-1}";
}
return readStr;
}