154 lines
6.4 KiB
C#
154 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
namespace LadfDataApp
|
|
{
|
|
public static class HttpUtil
|
|
{
|
|
public static CookieContainer cookie = new CookieContainer(); // 用于记录访问网页时cookie数据
|
|
public static CookieCollection cookieCollection;
|
|
|
|
private static string ContentType = string.Empty;// "application/x-www-form-urlencoded";
|
|
private static string Accept = string.Empty;//"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
|
|
private static string UserAgent = string.Empty;//"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
|
|
public enum Method
|
|
{
|
|
POST = 0,
|
|
GET = 1
|
|
}
|
|
public static void setting(string contentType, string accept, string userAgent)
|
|
{
|
|
ContentType = contentType;
|
|
Accept = accept;
|
|
UserAgent = userAgent;
|
|
}
|
|
/// <summary>
|
|
/// post数据到指定的网址,获取cookie数据,和返回页
|
|
/// </summary>
|
|
public static HttpWebResponse Post(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, Method method)
|
|
{
|
|
return Post(url, postData, pmsDic, headerDic, method, out cookieCollection);
|
|
}
|
|
/// <summary>
|
|
/// post数据到指定的网址,获取cookie数据,和返回页
|
|
/// </summary>
|
|
public static HttpWebResponse Post(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, Method method, out CookieCollection cookieCollection)
|
|
{
|
|
HttpWebResponse httpWebResponse = null;
|
|
try
|
|
{
|
|
HttpWebRequest httpWebRequest;
|
|
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
|
httpWebRequest.CookieContainer = cookie;
|
|
httpWebRequest.ContentType = ContentType;
|
|
//httpWebRequest.Referer = url;
|
|
httpWebRequest.Accept = Accept;
|
|
httpWebRequest.UserAgent = UserAgent;
|
|
httpWebRequest.Method = method == Method.POST ? "POST" : "GET";
|
|
byte[] byteRequest = null;
|
|
if (headerDic != null)
|
|
{
|
|
foreach (var item in headerDic)
|
|
{
|
|
httpWebRequest.Headers.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(postData))
|
|
{
|
|
byteRequest = Encoding.Default.GetBytes(postData);
|
|
httpWebRequest.ContentLength = byteRequest.Length;
|
|
}
|
|
else
|
|
{
|
|
StringBuilder builder = new StringBuilder();
|
|
int i = 0;
|
|
foreach (var item in pmsDic)
|
|
{
|
|
if (i > 0)
|
|
builder.Append("&");
|
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
|
i++;
|
|
}
|
|
byteRequest = Encoding.UTF8.GetBytes(builder.ToString());
|
|
httpWebRequest.ContentLength = byteRequest.Length;
|
|
}
|
|
Stream stream = httpWebRequest.GetRequestStream();
|
|
stream.Write(byteRequest, 0, byteRequest.Length);
|
|
stream.Close();
|
|
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
|
|
|
cookieCollection = cookie.GetCookies(new Uri(url));
|
|
return httpWebResponse;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
cookieCollection = null;
|
|
return httpWebResponse;
|
|
}
|
|
}
|
|
public static HttpWebResponse Post(string url, string postData, Dictionary<string, string> dic, Method method, out CookieCollection cookieCollection, out string result)
|
|
{
|
|
result = "";
|
|
HttpWebResponse httpWebResponse = null;
|
|
try
|
|
{
|
|
HttpWebRequest httpWebRequest;
|
|
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
|
httpWebRequest.CookieContainer = cookie;
|
|
httpWebRequest.ContentType = ContentType;
|
|
//httpWebRequest.Referer = url;
|
|
httpWebRequest.Accept = Accept;
|
|
httpWebRequest.UserAgent = UserAgent;
|
|
httpWebRequest.Method = method == Method.POST ? "POST" : "GET";
|
|
byte[] byteRequest = null;
|
|
if (!string.IsNullOrEmpty(postData))
|
|
{
|
|
byteRequest = Encoding.Default.GetBytes(postData);
|
|
httpWebRequest.ContentLength = byteRequest.Length;
|
|
}
|
|
else
|
|
{
|
|
StringBuilder builder = new StringBuilder();
|
|
int i = 0;
|
|
if (dic != null)
|
|
{
|
|
foreach (var item in dic)
|
|
{
|
|
if (i > 0)
|
|
builder.Append("&");
|
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
|
i++;
|
|
}
|
|
}
|
|
byteRequest = Encoding.UTF8.GetBytes(builder.ToString());
|
|
httpWebRequest.ContentLength = byteRequest.Length;
|
|
}
|
|
Stream stream = httpWebRequest.GetRequestStream();
|
|
stream.Write(byteRequest, 0, byteRequest.Length);
|
|
stream.Close();
|
|
|
|
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
|
Stream responseStream = httpWebResponse.GetResponseStream();
|
|
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
|
result = streamReader.ReadToEnd();
|
|
streamReader.Close();
|
|
responseStream.Close();
|
|
|
|
cookieCollection = cookie.GetCookies(new Uri(url));
|
|
return httpWebResponse;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
cookieCollection = null;
|
|
return httpWebResponse;
|
|
}
|
|
}
|
|
}
|
|
}
|