Baidu HttpWebRequest

7885 ワード

tcpTrace、httpLookを利用して、やっとできたので、バックアップをして、後で自分で価格を調整するソフトウェアを開発することができます.httpClientを使う予定でしたが、httpClientはPOSTをサポートしていないのか、Connection:Keep-Aliveをサポートしていないのか.どうせhttpClientリクエストで失敗した.client.ServicePoint.Expect100Continue = false; これはどんな属性で、私を3 hの時間にしました.バックアップして、後で自分で使います.
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();