GetResponse() 抛出异常 500错误

xikuang21 2012-03-24 05:23:27
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = true;
request.CookieContainer = cc;
request.KeepAlive = true;
//request.v

// myHttpWebRequest.Referer = "http://order.xiaomi.com/site/login";
//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
//Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
}

以上是代码,测试人人网通过了 但是过了一个网站就不行了 就总是出错
...全文
356 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-03-25
  • 打赏
  • 举报
回复
你用Http Watch等工具查看浏览器发送的Http头信息都是什么,你的程序也照着发送
xikuang21 2012-03-24
  • 打赏
  • 举报
回复
调用时 loginUrl="http://passport.xiaomi.com/index.php?appid=8&goto=http%3A%2F%2Forder.xiaomi.com%2Fsite%2Fcallback%3Freferer%3Dhttp%253A%252F%252Fwww.xiaomi.com%252F"; 这样用可以吗?
因为我测试的人人网 可以登录成功
xikuang21 2012-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 net_lover 的回复:]

request.UserAgent="Mozilla/5.0 (Windows NT 5.2; rv:11.0) Gecko/20100101 Firefox/11.0"
要加
[/Quote]
在调用的时候 加了的 不行
孟子E章 2012-03-24
  • 打赏
  • 举报
回复
request.UserAgent="Mozilla/5.0 (Windows NT 5.2; rv:11.0) Gecko/20100101 Firefox/11.0"
要加

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧