通过Fiddler或Postman请求从调试模式请求时,两者都返回成功结果,它总是返回内部服务器错误(500)。
到目前为止我做了什么:
使用Fiddler v4.6.20172.34691: 以下是示例请求: Fiddler request
以下是示例回复: Fiddler response
使用Postman v6.1.3: 以下是示例请求和响应: Postman request and reponse
对于visual studio部分: Form1代码初始化部分:
urlparameters = @"?account=" + username;
url = "https://" + platform + "/Home/CheckRegAccountAvailable" + urlparameters;
method = "GET";
clsHttp.cs初始化httpwebrequest:
request = (HttpWebRequest)WebRequest.Create(_url);
request.Timeout = _timeout;
request.Method = _method;
request.ContentType = _responsetype;
if (_method != "GET")
{
if (_contentlength > 0)
{
request.ContentLength = _contentlength;
}
if (_isrequestbody)
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(_requestbody);
streamWriter.Flush();
streamWriter.Close();
}
}
}
clsHttp.cs获得回复:
string result = "", _msg = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
_msg = "Received response...";
Console.WriteLine(_msg);
if (_isLog)
doLogToFile(_filename, _fileextension, _path, _msg);
if (_isUIRTBLog)
rtb_AppendText(_rtb, _msg + "\r\n");
Console.WriteLine(_msg);
}
}
提前致谢。
[更新1]
似乎设置用户代理有效,出于某种原因这是服务器问题吗?
[更新2]
下面是示例WebException.Response如果我没有包含返回内部服务器错误的用户代理(500),我会得到:
CharacterSet:" utf-8" ContentEncoding:"" ContentLength:36213 ContentType:" text / html;字符集= UTF-8" Cookies:{System.Net.CookieCollection}标题:{Content-Length:36213 Cache-Control:private Content-Type:text / html; charset = utf-8 Set-Cookie:ASP.NET_SessionId = vjxdzvk43vxif4kkviib5vio;路径= /;仅Http,语言环境= ZH-CN;路径= /;仅Http,ASP.NET_SessionId = vjxdzvk43vxif4kkviib5vio;路径= /;仅Http,语言环境= ZH-CN;路径= /;仅Http,__ RequestVerificationToken = MjSUG0cF0M-T2dE9BmthuQq_8vWLgdne0xPuzZggdMst1jUwfuZ30mDzwAdXFSQBMP8ql9VdbXC-d0cLvgpBiCydvrRk8lvZrStzp6e7cyQ1;路径= /;仅Http,ARRAffinity = e55696f2f9da07a0a50d293b25a04f89be0f0552dbf622b9a924c6d5d272eff4;路径= /;的HttpOnly; Domain = www.iwin98.com服务器:Microsoft-IIS / 10.0 X-AspNetMvc-版本: 5.2 X-Frame选项:SAMEORIGIN,SAMEORIGIN,SAMEORIGIN X-AspNet-版本:4.0.30319 X-Powered-By:ASP.NET日期:星期二,2018年5月29日01:19:08 GMT
}
IsFromCache:false IsMutuallyAuthenticated:false LastModified:{5/29/2018 9:19:17 AM}方法:" GET" ProtocolVersion:{1.1} ResponseUri:{https://www.iwin98.com/Home/CheckRegAccountAvailable?account=username}服务器:" Microsoft-IIS / 10.0" StatusCode:InternalServerError StatusDescription:"内部服务器错误" SupportsHeaders:true
答案 0 :(得分:0)
您的代码未设置User-Agent请求标头,并且所请求的URL可能不是智能的,因此期望没有User-Agent的请求。它可以在内部抛出NullReferenceException。
将此添加到您的代码中:
request.UserAgent="Some value"
如果您可以控制服务器,请修复服务器。