问题描述
- 有赞模拟登陆返回500错误 1C
- 我用浏览器打开时正常的,但是在程序中为什么就返回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 MyCookieshttp://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(urlPOST"");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();}}}
解决方案
url是
http://koudaitong.com/v2/account/user/login.json
时间: 2024-08-04 05:46:01