首页新闻找找看学习计划

有赞模拟登陆返回500错误

0
悬赏园豆:50 [已关闭问题] 关闭于 2015-01-15 11:54

我用浏览器打开时正常的,但是在程序中为什么就返回500错误了呢?具体代码如下:

地址:http://koudaitong.com/v2/account/user/login

/// <summary>
/// 获取验证码

private void 登陆_Load(object sender, EventArgs e)
{
txtUserName.Text="18076549077";
txtPwd.Text="123456789";
GetViliCode();

}
private void pbCode_Click(object sender, EventArgs e)
{
GetViliCode();
}

/// <summary>
/// 获取验证码
/// </summary>
public void GetViliCode()
{
var codeStr = "http://koudaitong.com/captcha";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(codeStr);
request.CookieContainer = MyCookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();//得到验证码数据流
Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
this.pbCode.Image = new Bitmap(sourcebm);
}

/// <summary>
/// 登陆
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text;
string pwd = txtPwd.Text;
string code = txtCode.Text;
IDictionary<string, string> loginParameter = new Dictionary<string, string>();
loginParameter.Add("account", userName);
loginParameter.Add("password", pwd);
loginParameter.Add("captcha_code", code);
loginParameter.Add("auto_login", "on");
if (userName != "" && pwd != "" && code != "")
{
try
{
var cc = DoPost(loginUrl, loginParameter, ref MyCookies, "http://koudaitong.com/v2/account/user/login");
}
catch (Exception ex)
{
var bb = ex.Message;
}
}
}

public string DoPost(string url, IDictionary<string, string> parameters, ref CookieContainer cookieContainer, string referer)
{
HttpWebRequest req = GetWebRequest(url, "POST");
System.Net.ServicePointManager.DefaultConnectionLimit = 2000;
if (cookieContainer != null)
{
req.CookieContainer = cookieContainer;
}
if (!string.IsNullOrEmpty(referer))
{
req.Referer = referer;
}

byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters));
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
req.ContentLength = postData.Length;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
req.Accept = "application/json, text/javascript, */*; q=0.01";

Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
reqStream.Close();

HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
//垃圾回收
// System.GC.Collect();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}

/// <summary>
/// 组装普通文本请求参数。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
public static string BuildQuery(IDictionary<string, string> parameters)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
if (parameters != null)
{
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;

if (hasParam)
{
postData.Append("&");
}

postData.Append(name);
postData.Append("=");
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
hasParam = true;
//}
}
}

return postData.ToString();
}

}

}

 

 

 

noert的主页 noert | 初学一级 | 园豆:43
提问于:2015-01-15 09:45
< >
分享
所有回答(3)
0

肯定是报文不对了。自己对比下报文

吴瑞祥 | 园豆:28731 (高人七级) | 2015-01-15 09:53

对过了,没有错啊!还会有什么问题会导致这个错误呢?我把参数去掉或输错的验证码或账号返回的都是500错误,但是在浏览器这个是可以体现出具体的错误来的。

支持(0) 反对(0) noert | 园豆:43 (初学一级) | 2015-01-15 10:17
0

搞了半天原来是地址错了:http://koudaitong.com/v2/account/user/login.json

noert | 园豆:43 (初学一级) | 2015-01-15 11:53
0

你搞定了嘛?我能登陆到时无法进入店铺,请教一下。加qq:9791377,多谢。

sunshuai0312 | 园豆:202 (菜鸟二级) | 2015-10-26 21:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册