最近项目用到的一个业务分包发送技术点,现在记录下来,方便以后回顾
1、工具Fiddler4(抓包工具)
2、VS2010
提交核心方法:
/// <summary>
/// 使用webrequest进行数据提交
/// </summary>
/// <param name="url">提交的地址</param>
/// <param name="url_referer">提交的源地址</param>
/// <param name="data">提交的数据</param>
/// <param name="Mehtod">提交模式</param>
/// <returns></returns>
public static string postHttpC(string url, string url_referer, string data, string Mehtod = "POST", bool xml = false, string head = "")
{//encodeURI
if (State) return "提交停止";
System.GC.Collect();
HttpWebRequest httpwebRequest = null;
HttpWebResponse httpwebResponse = null;
StreamReader streamReader = null;
string responsecontent = "";
try
{
httpwebRequest = (HttpWebRequest)WebRequest.Create(Ip + url);
if (url_referer != "" && url_referer != null)
{
httpwebRequest.Referer = Ip + url_referer;
}
httpwebRequest.Method = Mehtod;
if (!head.Equals("")) { httpwebRequest.ContentType = head; }
else
{
httpwebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; //text / plain;
}
httpwebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*";
httpwebRequest.UserAgent = "Mozilla/4.0Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/8.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Tablet PC 2.0)";
//image/gif, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
httpwebRequest.Headers["Cache-Control"] = "no-cache";
httpwebRequest.Headers["Accept-Language"] = "zh-CN";
httpwebRequest.Headers["Accept-Encoding"] = "gzip, deflate";
if (xml)
{
httpwebRequest.Headers["X-Requested-With"] = "XMLHttpRequest";
//httpwebRequest.Headers["aaxmlrequest"] = "true";
}
httpwebRequest.KeepAlive = true;
httpwebRequest.Headers["Cookie"] = Cookie;//赋值cookeis
Encoding encoding = Encoding.GetEncoding("utf-8");
if (data != "" && data != null)
{
byte[] postData = encoding.GetBytes(HttpUtility.UrlEncode(data, Encoding.UTF8).setenCode());
httpwebRequest.ContentLength = postData.Length;
Stream requeStream = httpwebRequest.GetRequestStream();
requeStream.Write(postData, 0, postData.Length);
}
httpwebResponse = (HttpWebResponse)httpwebRequest.GetResponse();
if (httpwebResponse.CharacterSet.ToUpper().Equals("UTF-8"))
{
encoding = Encoding.Unicode;
}
streamReader = new StreamReader(httpwebResponse.GetResponseStream());
responsecontent = streamReader.ReadToEnd();
}
catch { return "提交异常"; }
finally
{
if (httpwebResponse != null)
{
httpwebResponse.Close();
}
if (streamReader != null)
{
streamReader.Close();
}
if (httpwebRequest != null)
{
httpwebRequest.Abort();
}
httpwebRequest = null;
httpwebResponse = null;
streamReader = null;
}
return responsecontent;
}
抓包头部信息获取