读书人

模拟post提交注册出现有关问题验证码

发布时间: 2012-11-03 10:57:44 作者: rapoo

模拟post提交注册出现问题,验证码总说不正确,怎么办啊,真的是很急
我用下面代码下载到的验证码
HttpWebRequest h = (HttpWebRequest)HttpWebRequest.Create("http://9.52ms.org/vpn/include/vdimgck.php");

HttpWebResponse response = (HttpWebResponse)h.GetResponse();
pictureBox1.Image = new Bitmap(response.GetResponseStream());
CookieContainer cookie = new CookieContainer();
cookie.Add(response.Cookies);

用的下面这个方法调用模拟post提交
string html = HttpHelper.GetHtml("http://9.52ms.org/vpn/useradmin/user_self_new_save.php", null, "POST", "ioBB=&regsdid=&chksdid=false&hidRecName001=&hidRecName002=&hidRecRule001=&hidRecRule002=&agent4IpCheck=checkIp&txtRegisterFrom=0&txtRegisterZone=0&sessionid=lr1c1w55vih4et45ct511krb&goldtype=&partner=&apptype=&agent=&ctype=&hidExinfo=&qqaccount=&CUSTOM_REG_EVN=&ptType=0&UserID=aab2" + i + "&Upassword=123123&Upassword2=123123&Email_Address=123@qq.com&User_True=&User_QQ=&idcard=&Description=&ThisNo="+textBox1.Text+"&btnSubmit=+%E7%AB%8B%E5%8D%B3%E6%B3%A8%E5%86%8C91vpn%E8%B4%A6%E5%8F%B7+");
MessageBox.Show(html);下面是post提交的方法。输入的验证码不一样,提示错误,请大家指示。为什么?难道这样模拟获取的验证码,没有效果么?
听说是什么cookies的问题?怎么整?
public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
{
cookieContainer = cookieContainer ?? new 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 = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;

if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
}

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();

streamReader.Close();
responseStream.Close();

httpWebRequest.Abort();
httpWebResponse.Close();

return html;
}
catch (Exception)
{
return string.Empty;
}
}

[解决办法]

C# code
    public class HttpHelper    {        public static string contentType = "application/x-www-form-urlencoded";        public 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, */*";        public 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://9.52ms.org/vpn/useradmin/user_self_new.php";        /// <summary>        /// 获取字符流        /// </summary>        /// <param name="url"></param>        /// <param name="cookieContainer"></param>        /// <returns></returns>        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;            }        }        /// <summary>        /// 获取HTML        /// </summary>        /// <param name="url"></param>        /// <param name="cookieContainer"></param>        /// <param name="method">GET or POST</param>        /// <param name="postData">like "username=admin&password=123"</param>        /// <returns></returns>        public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")        {            cookieContainer = cookieContainer ?? new 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 = method;                httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;                if (method.ToUpper() == "POST")                {                    byte[] byteRequest = Encoding.Default.GetBytes(postData);                    Stream stream = httpWebRequest.GetRequestStream();                    stream.Write(byteRequest, 0, byteRequest.Length);                    stream.Close();                }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();                string cookie_string = httpWebResponse.Headers.Get("Set-Cookie");                cookie_string += "0";                Stream responseStream = httpWebResponse.GetResponseStream();                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);                string html = streamReader.ReadToEnd();                streamReader.Close();                responseStream.Close();                httpWebRequest.Abort();                httpWebResponse.Close();                return html;            }            catch (Exception)            {                return string.Empty;            }        }    } 

读书人网 >C#

热点推荐