百度调价HttpWebRequest

利用了tcpTrace,httpLook,好不容易搞定了,做下备份,以后就可以自己开发调价的软件了。原来准备用httpClient,但是httpClient是不支持POST,还是不支持Connection: Keep-Alive。反正用httpClient请求失败了。client.ServicePoint.Expect100Continue = false; 这个什么属性,弄了我3h的时间。备份下,以后自己用。

Dictionary<string, string> cookiesTable = this.ConvertCookies(this.currentCookie);
StringBuilder sb = new StringBuilder("path=");
sb.Append(System.Web.HttpUtility.UrlEncode("MOD/").ToUpper());
sb.Append("keyword");
sb.AppendFormat("&userid={0}", cookiesTable["__cas__id__3"]);
sb.AppendFormat("&token={0}", cookiesTable["__cas__st__3"]);
sb.Append("&params={\"winfoid\":[\"1492927264\"],\"items\":{\"bid\":\"{price}\"}}".Replace("{price}", this.textBox1.Text));
string postData = sb.ToString();
byte[] data = System.Text.Encoding.UTF8.GetBytes(postData);

HttpWebRequest client = (HttpWebRequest)HttpWebRequest.Create("http://fengchao.baidu.com/nirvana/request.ajax");

client.Headers.Clear();

//client.Headers.Add(HttpRequestHeader.Accept, "*/*");
client.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
//client.Headers.Add(HttpRequestHeader.Host, "fengchao.baidu.com");
//client.Headers.Add(HttpRequestHeader.Referer, "http://fengchao.baidu.com/nirvana/main.html?userid=797912#/manage/");
client.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3");
//client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; .NET4.0C; InfoPath.2)");
client.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
//client.Headers.Add(HttpRequestHeader.ContentLength, postData.Length.ToString());
//client.Headers.Add(HttpRequestHeader.Connection, "Keep-Alive");
//client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
//client.Headers.Add(HttpRequestHeader.Cookie, this.currentCookie);

client.Accept = "*/*";
client.Referer = "http://fengchao.baidu.com/nirvana/main.html?userid=797912#/manage/";
client.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; .NET4.0C; InfoPath.2)";
//client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
client.ContentLength = data.Length;
client.KeepAlive = true;
client.ServicePoint.Expect100Continue = false;
client.ContentType = "application/x-www-form-urlencoded";
client.Headers.Add("x-request-by: baidu.ajax");
client.Method = "POST";
client.Headers.Add(HttpRequestHeader.Cookie, this.currentCookie);

System.IO.Stream sw0 = client.GetRequestStream();
sw0.Write(data, 0, data.Length);


HttpWebResponse response = (HttpWebResponse)client.GetResponse();
System.IO.Stream sw = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(sw, System.Text.Encoding.UTF8);
string result = reader.ReadToEnd();
System.Diagnostics.Trace.Write(result);
reader.Close();
sw0.Close();
sw.Close();
posted @ 2011-12-30 17:31  BigRain  阅读(1190)  评论(1编辑  收藏  举报