CookieContainer myCookieContainer = new CookieContainer(); //string[] cookstr = cookieStr.Split(';'); //CookieCollection cookieCollection = new CookieCollection(); //foreach (string str in cookstr) //{ // string name = str.Substring(0, str.IndexOf("=")); // string value = str.Substring(str.IndexOf("=") + 1); // Cookie ck = new Cookie(name, value); // cookieCollection.Add(ck); //} //myCookieContainer.Add(new Uri("www.58.com"), cookieCollection); myCookieContainer.SetCookies(new Uri("www.58.com"), cookieStr); string content = string.Empty; imgUrlList.ForEach(delegate(string url) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; request.KeepAlive = true; request.Accept = "*/*"; request.Referer = "http://post.58.com/541/8/s5?pts=1403055598929"; request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch"); request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3"); request.CookieContainer = myCookieContainer; Stream Stream; Stream = request.GetRequestStream(); //Stream.Write(streamByImg, 0, streamByImg.Length);
如上边所示代码,我尝试了很多方式来将HttpRequest请求的CookieContainer属性赋值,发下怎么弄都无法实现,在我注释的那段里,
Cookie ck = new Cookie(name, value);
我用直接new Cookie的方式,提交,发现第一次new的时候可以,第二次的时候就直接没有异常,直接退出该方法的执行了,没有执行 cookieCollection.Add(ck);方法,请问,这是为什么?下班是登陆后的cookie
58home=km; city=km; id58=05dvOVOf1PxY+V/4GgaUAg==;
__utma=253535702.1501813282.1403074124.1403074124.1403074124.1; __utmz=253535702.1403074124.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);
myfeet_tooltip=end; bangtoptipclose=1; __ag_cm_=1402989921255;
final_history=18271258772353%2C18278492711553%2C18190325787011%2C18278384137986%2C18301407806082;
__utmb=253535702.60.8.1403077625021;
new_session=0;
init_refer=http%253A%252F%252Fmy.58.com%252F%253Fpts%253D1403069417701;
__utmv=253535702.|1=MemberType=VIP=1;
__utmc=253535702;
CNZZDATA30017898=cnzz_eid%3D794545735-1402983681-null%26ntime%3D1403079609;
wxtk=20484867;
wxtksec=259815937170
request.Headers.Add("Cookie", cookieStr);
我用了你这种方法,发下到了Stream = request.GetRequestStream();这一步,直接跳出了,也不报错,没有往下边执行了,这是提交的请求内容有问题吗?
@就叫我好人吧: 你能给我演示下什么叫着“直接跳出了,也不报错”吗?
@Launcher:
public string downImgFor58(List<string> urlImg) { //CookieContainer myCookieContainer = new CookieContainer(); //string[] cookstr = cookieStr.Split(';'); //CookieCollection cookieCollection = new CookieCollection(); //foreach (string str in cookstr) //{ // string name = str.Substring(0, str.IndexOf("=")); // string value = str.Substring(str.IndexOf("=") + 1); // Cookie ck = new Cookie(name, value); // cookieCollection.Add(ck); //} //myCookieContainer.Add(new Uri("www.58.com"), cookieCollection); //myCookieContainer.SetCookies(new Uri("www.58.com"), cookieStr); string content = string.Empty; imgUrlList.ForEach(delegate(string url) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET";//这里改为post 后面是你想要post的数据,更多参数的设置在header建议看看msdn request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; request.KeepAlive = true; request.Accept = "*/*"; request.Referer = "http://post.58.com/541/8/s5"; request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch"); request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3"); //request.CookieContainer = myCookieContainer; //request.Headers.Add("Cookie", cookieStr); Stream Stream; Stream = request.GetRequestStream(); //Stream.Write(streamByImg, 0, streamByImg.Length); Stream.Close(); HttpWebResponse responseSorce = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8); reader.ReadToEnd(); }); return @"http://pic.58.com/p1/tiny/" + content; }
这本来是一个方法嘛,在方法执行到Stream = request.GetRequestStream();这一步,他就没有执行Stream.Close();以下的代码了。
@就叫我好人吧: 你确定没有抛出异常吗?你不需要 try catch 一下?