1019-003-gaofei
This commit is contained in:
@@ -145,5 +145,73 @@ namespace BLL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不做catch处理,需要在外部做
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="method">默认GET,空则补充为GET</param>
|
||||
/// <param name="contenttype">默认json,空则补充为json</param>
|
||||
/// <param name="header">请求头部</param>
|
||||
/// <param name="data">请求body内容</param>
|
||||
/// <returns></returns>
|
||||
public static string ControlHttp(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null)
|
||||
{
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
|
||||
request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
|
||||
if (header != null)
|
||||
{
|
||||
foreach (var i in header.Keys)
|
||||
{
|
||||
request.Headers.Add(i.ToString(), header[i].ToString());
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(data))
|
||||
{
|
||||
Stream RequestStream = request.GetRequestStream();
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||||
RequestStream.Write(bytes, 0, bytes.Length);
|
||||
RequestStream.Close();
|
||||
}
|
||||
|
||||
HttpWebResponse response = null;
|
||||
Stream ResponseStream = null;
|
||||
StreamReader StreamReader = null;
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
ResponseStream = response.GetResponseStream();
|
||||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||||
|
||||
string re = StreamReader.ReadToEnd();
|
||||
return re;
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
response = (HttpWebResponse)ex.Response;
|
||||
ResponseStream = response.GetResponseStream();
|
||||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||||
|
||||
string re = StreamReader.ReadToEnd();
|
||||
return re;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (StreamReader != null)
|
||||
{
|
||||
StreamReader.Close();
|
||||
}
|
||||
if (ResponseStream != null)
|
||||
{
|
||||
ResponseStream.Close();
|
||||
}
|
||||
if (response != null)
|
||||
{
|
||||
response.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user