post验证码始终失败,请问大家什么原因:
执行过程是:
1.下载验证码到本地,并获取cookie值。
2.附加第一步生成的cookie,post过去提交。
执行代码:
[C#] 纯文本查看 复制代码 string url = "http://www.plateno.com/imageToken?id=registry-checkcode&r=" + DateTime.Now.Ticks.ToString();
string validatpath = "E:/123.jpg";
string cookie = string.Empty;
//下载验证码到本地并返回服务断的cookie
bool b = Utility.HttpWebResponseUtility.DownValidateCodeImg(url, null, validatpath, out cookie);
//构造请求参数
HttpHelper hh = new HttpHelper();
HttpItem item = new HttpItem();
item.Method = "Post";
item.PostEncoding = Encoding.UTF8;
string imgCode = DCHelper.GetImgValidateCode(validatpath, "liaoxinklx", "liaoxin349881223");
Dictionary<string, string> postDic = new Dictionary<string, string>();
postDic.Add("sellerId", "208624");
postDic.Add("regSourceId", "12");
postDic.Add("memberName", "赖世萍");
postDic.Add("docNo", "440107196410190310");
postDic.Add("password", "abc123");
postDic.Add("passwordAgain", "abc123");
postDic.Add("email", "");
postDic.Add("checkcode", imgCode);
postDic.Add("atk", "abcdefg");
postDic.Add("agreeTerms", "on");
item.PostDic = postDic;
item.Cookie = cookie;
item.ResultCookieType = ResultCookieType.CookieCollection;
item.ContentType = "application/x-www-form-urlencoded";
item.URL = "http://www.plateno.com/member/registry";
HttpResult hr=hh.GetHtml(item);
下载验证码:
public static bool DownValidateCodeImg(string url, Dictionary<string, string> parameters, string savePath,out string cookie)
{
cookie = string.Empty;
if (parameters != null && parameters.Count > 0)
{
if (url.Contains("?"))
{
url = url + "&" + BuildPostData(parameters);
}
else
{
url = url + "?" + BuildPostData(parameters);
}
}
bool bol = true;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//属性配置
webRequest.KeepAlive = true;
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
//webRequest.CookieContainer = cookCon;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
cookie = webResponse.Headers[HttpResponseHeader.SetCookie];
using (Stream sream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = sream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes(savePath, list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
}
catch (Exception ex)
{
bol = false;
}
return bol;
}
|