200分求教:webRequest 如何获取和保持cookies!
需求:想在内部系统加入google检索,用webrequest获取结果,然后按我自己的格式显示结果。我想让google按照http://www.google.cn/preferences?hl=zh-CN中设置,返回结果。
最好能用WebRequest获取cookies然后,每次检索都带上cookies,最好不用webbrowse.
请高手帮助!
[解决办法]
http://www.cnblogs.com/goody9807/archive/2008/06/26/1107560.html
[解决办法]
WebRequest是获取一个url即外界的信息的,cookie是你本机上的东西,检索的时候带上cookie是什么意思呢 ?
[解决办法]
程序想网页传递参数用post
HttpWebRequest w = (HttpWebRequest)WebRequest.Create("http://www.google.cn");CookieContainer cookie = new CookieContainer();w.CookieContainer = cookie;//...
[解决办法]
public string Login(string url, string usr, string pass) { string Return = null; this.LoginUrl = url; string loginstr = "rl=%2Fhome%2F&email=" + usr + "&password=" + pass; loginstr = EncodePost(loginstr); byte[] replybyte = Encoding.UTF8.GetBytes(loginstr); try { CookieContainer sparkc = new CookieContainer(); SparkRequest = (HttpWebRequest)WebRequest.Create(url); SparkRequest.CookieContainer = sparkc; SparkRequest.ContentType = "application/x-www-form-urlencoded"; SparkRequest.Method = "POST"; SparkRequest.ContentLength = replybyte.Length; Stream newStream = SparkRequest.GetRequestStream(); newStream.Write(replybyte, 0, replybyte.Length); newStream.Close(); SparkResponse = (HttpWebResponse)SparkRequest.GetResponse(); Stream dataStream = SparkResponse.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("UTF-8")); Return = reader.ReadToEnd(); foreach (Cookie temp in SparkResponse.Cookies) { if (temp.Domain != "www.xxxxxxx.com") temp.Domain = "www.xxxxxxxxxx.com"; } CkCollection = SparkResponse.Cookies; // 这句 } catch { return null; } return Return; }
[解决办法]
学习
[解决办法]
HttpWebRequest hreq=null;
CookieContainer cCookie = new CookieContainer();
private string SendRequest(string _url,string _requestString)
{
try
{
byte[] requestBytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(_requestString);
hreq= (HttpWebRequest)WebRequest.Create(_url);
hreq.ContentType = "application/x-www-form-urlencoded";
hreq.ContentLength= requestBytes.Length;
hreq.Method = "POST";
hreq.CookieContainer = cCookie;
using(Stream writer = hreq.GetRequestStream())
{
writer.Write(requestBytes,0,requestBytes.Length);
}
}
[解决办法]
学到 不少东西
[解决办法]
hreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)";
//或 hreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
//或 hreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
是否频繁获取,有可能被禁用