代码
/// <summary> /// 获取网页验证码 /// </summary> /// <param name="server"> 服务器地址 </param> /// <param name="URL"> 网页地址 </param> /// <param name="byteRequest"> 字节数组,存放图片字节 </param> /// <param name="cookie"> 网站分配给客户的临时sessionid(临时cookie值),并非真正cookie </param> /// <param name="header"></param> /// <returns> 图片的二进制数组 </returns> public static byte [] GetHtmlByBytes( string server, string URL, byte [] byteRequest, string cookie, out string header) { long contentLength; HttpWebRequest httpWebRequest; HttpWebResponse webResponse; Stream getStream; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL); CookieContainer co = new CookieContainer(); co.SetCookies( new Uri(server), cookie); httpWebRequest.CookieContainer = co; httpWebRequest.ContentType = " application/x-www-form-urlencoded " ; httpWebRequest.Method = " Post " ; httpWebRequest.ContentLength = byteRequest.Length; Stream stream; stream = httpWebRequest.GetRequestStream(); stream.Write(byteRequest, 0 , byteRequest.Length); stream.Close(); webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); header = webResponse.Headers.ToString(); getStream = webResponse.GetResponseStream(); contentLength = webResponse.ContentLength; byte [] outBytes = new byte [contentLength]; outBytes = ReadFully(getStream); getStream.Close(); return outBytes; }