HttpWebRequest通过代理如何下载没有后缀的图片
<img id="codeimage" src="wap/qhai/checkimage" height="20" width="55"/>这个一个验证码图片的标签,用HttpWebRequest去下载验证码,服务器返回404,下载代码如下:public void RunImage(int i,string uri)
{
System.Net.HttpWebRequest Request = null;
System.Net.HttpWebResponse Response = null;
System.IO.StreamReader S = null;
try
{
Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
WebProxy loProxy = new WebProxy("http://10.0.0.172:80", true);
Request.Proxy = loProxy;
Request.Referer = frmStatrWap.url;//
Response = (System.Net.HttpWebResponse)Request.GetResponse();
string encod = Response.CharacterSet;
if (Response.ContentType.IndexOf("image") > -1)
{
Stream str = Response.GetResponseStream();
FileStream fs = new FileStream(@"D:\image\" + i + ".jpg", FileMode.OpenOrCreate);
int num = 512;
byte[] nbytes = new byte[num];
int nReadSize = str.Read(nbytes, 0, nbytes.Length);
while (nReadSize > 0)
{
//allbyte = AppendOct(nbytes, allbyte);//这个可以去掉
fs.Write(nbytes, 0, nReadSize);
nReadSize = str.Read(nbytes, 0, nbytes.Length);
fs.Flush();
}
fs.Close();
str.Close();
//img = Image.FromStream(Response.GetResponseStream());
}
} --------------------编程问答-------------------- 新开一个IE,复制图片地址是否能打开
有的生成图片的页面,判断了来源或者COOKIE,没有的话,是打不开的,需要伪造 --------------------编程问答-------------------- 回复 拿分 走人 就是打发士大夫 是打发士大夫发生的发生的发生 --------------------编程问答-------------------- noyester 你说的cookies我有添加进去的,添加cookie代码Request.Headers["Cookie"]=imageCookie;这个imageCookie是我从第一次请求中取出来的!下载验证码图片的请求代码:
Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
Request.Headers["Cookie"] = frmStatrWap.imageCookie;
WebProxy loProxy = new WebProxy("10.0.0.172", 80);
Request.UseDefaultCredentials = true;
Request.Proxy = loProxy;
Request.Method = "GET";
Request.Referer = frmStatrWap.url; --------------------编程问答-------------------- 用Fiddle抓包后一个字节一个字节的对比,如果你的程序发的包和浏览器发的一样了,没理由打不开.另外,如果抓包的页面设计到二级域名,.NET自带的cookie会有各种奇怪的问题发生. --------------------编程问答-------------------- 我没有给Request添加cookie,只是在Headers里面设置了cookieId;Request.Headers["Cookie"] = frmStatrWap.imageCookie; --------------------编程问答-------------------- 没有人会搞吗?结贴了! --------------------编程问答-------------------- 如果单独用IE打开可以看到图片,那说明你的请求代码有问题,
你要设置请求内容类型,即要设置 Content-Type,至于是什么值,你可以打开浏览器监控一下,看看请求图片时用的是什么Content-Type。 --------------------编程问答-------------------- 这是我获取QQ验证码的代码:
private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
public static string referer = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html";
public static Stream GetStream(string url, CookieContainer cookieContainer)
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET";
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
return responseStream;
}
catch (Exception)
{
return null;
}
}
直接这样使用就OK:img = Image.FromStream(Stream );
你参考参考。。。 --------------------编程问答-------------------- 我这边下载的验证码是手机wap网端的验证码。楼上的方法是下载不下来的!
补充:.NET技术 , C#