• 百度调价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();
  • 相关阅读:
    jstl标签的fmt:formatDate格式化日期 String to Date
    Spring MVC使用ModelAndView进行重定向
    配置SpringAop时需要用到的AspectJ表达式
    深入分析Java Web中的编码问题
    第六十五条:不要忽略异常
    第六十四条:努力使失败保持原子性
    第六十三条:在细节消息中包含能捕获失败的信息
    第六十二条:每个方法抛出的异常都要有文档
    第六十一条:抛出与抽象相对应的异常
    第六十条:优先使用标准的异常
  • 原文地址:https://www.cnblogs.com/Linjianyu/p/2307783.html
Copyright © 2020-2023  润新知