160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace HZQR.Common
|
|
{
|
|
public class HttpUtil
|
|
{
|
|
/// <summary>
|
|
/// 创建POST方式的HTTP请求
|
|
/// </summary>
|
|
/// <param name="url">请求路径</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <param name="userAgent">代理</param>
|
|
/// <param name="cookies">cookie</param>
|
|
/// <param name="timeout">超时时间</param>
|
|
/// <param name="headParam">请求头</param>
|
|
/// <returns></returns>
|
|
|
|
public static string HttpUrlPost(string url, IDictionary<string, string> parameters,
|
|
string userAgent = null, CookieCollection cookies = null, int timeout = 200,
|
|
IDictionary<string, string> headParam = null, string contentType = "application/x-www-form-urlencoded")
|
|
{
|
|
string reString = "";
|
|
|
|
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
|
#region http请求带请求头部分
|
|
if (!(headParam == null || headParam.Count == 0))
|
|
{
|
|
foreach (string key in headParam.Keys)
|
|
{
|
|
request.Headers.Add(key, headParam[key]);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
request.Method = "POST";
|
|
request.ContentType = contentType;
|
|
//设置代理UserAgent和超时
|
|
request.UserAgent = userAgent;
|
|
if (timeout > 0)
|
|
{
|
|
request.Timeout = timeout * 1000;
|
|
}
|
|
if (cookies != null)
|
|
{
|
|
request.CookieContainer = new CookieContainer();
|
|
request.CookieContainer.Add(cookies);
|
|
}
|
|
|
|
//发送POST数据
|
|
if (!(parameters == null || parameters.Count == 0))
|
|
{
|
|
if (contentType == "application/json")
|
|
{
|
|
request.KeepAlive = false;
|
|
JObject info = new JObject();
|
|
foreach (string key in parameters.Keys)
|
|
{
|
|
info[key] = parameters[key];
|
|
}
|
|
string postDataStr = info.ToString();
|
|
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
|
|
request.ContentLength = postBytes.Length;
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StringBuilder buffer = new StringBuilder();
|
|
int i = 0;
|
|
foreach (string key in parameters.Keys)
|
|
{
|
|
if (i > 0)
|
|
{
|
|
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
|
|
}
|
|
else
|
|
{
|
|
buffer.AppendFormat("{0}={1}", key, parameters[key]);
|
|
i++;
|
|
}
|
|
}
|
|
byte[] data = Encoding.ASCII.GetBytes(buffer.ToString());
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
System.Net.HttpWebResponse res = request.GetResponse() as HttpWebResponse;
|
|
if (res == null)
|
|
{
|
|
reString = "error";
|
|
}
|
|
else
|
|
{
|
|
//获取返回数据转为字符串
|
|
reString = GetResponseString(res);
|
|
}
|
|
return reString;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取请求的数据
|
|
/// </summary>
|
|
public static string GetResponseString(HttpWebResponse webresponse)
|
|
{
|
|
using (Stream s = webresponse.GetResponseStream())
|
|
{
|
|
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
|
return reader.ReadToEnd();
|
|
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Get请求
|
|
///
|
|
///
|
|
/// 字符串
|
|
public static string HttpUrlGet(string url, int timeout = 200)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = "GET";
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
request.UserAgent = null;
|
|
request.Timeout = timeout;
|
|
HttpWebResponse response;
|
|
try
|
|
{
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
response = (HttpWebResponse)ex.Response;
|
|
}
|
|
Stream myResponseStream = response.GetResponseStream();
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
string retString = myStreamReader.ReadToEnd();
|
|
myStreamReader.Close();
|
|
myResponseStream.Close();
|
|
|
|
return retString;
|
|
}
|
|
|
|
}
|
|
}
|