Ann*_*nna 2 .net c# exception-handling httpwebrequest internal-server-error
我试图自己解决这个问题,但我无法得到任何有用的解决方案.我想向网站发送请求并获得处理结果.我已经遇到了问题(请参阅如何填写网站表单并在C#中检索结果?)但我能够通过其中所述的网站解决它.现在我正尝试使用以下代码访问另一个网站(http://motif-x.med.harvard.edu/motif-x.html):
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://motif-x.med.harvard.edu/cgi-bin/multimotif-x.pl");
request.Credentials = CredentialCache.DefaultCredentials;
request.ProtocolVersion = HttpVersion.Version10; // Motif-X uses HTTP 1.0
request.KeepAlive = false;
request.Method = "POST";
string motives = "SGSLDSELSVSPKRNSISRTH";
string postData = "fgdata=" + motives + "&fgcentralres=S&width=21";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "multipart/form-data";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
这给了我以下例外:
远程服务器返回错误:(500)内部服务器错误.
有什么可以阻止这个吗?
如果您想手动输入网站:
数据集的文本区域: SGSLDSELSVSPKRNSISRTH
中心字符(基本选项):S
您将被重定向到结果的站点 - 可能需要一段时间才能处理.这可能是异常的原因吗?
如果您查看文档,您可以看到,对于"multipart/form-data",发送数据"POST"与"application/x-www-form-urlencoded"非常不同.对于你的情况,你必须发送这样的东西:
内容类型:
Content-Type: multipart/form-data; boundary=---------------------------7db1af18b064a
Run Code Online (Sandbox Code Playgroud)
POST:
-----------------------------7db1af18b064a
Content-Disposition: form-data; name="fgdata"
SGSLDSELSVSPKRNSISRTH
-----------------------------7db1af18b064a
Content-Disposition: form-data; name="fgcentralres"
S
-----------------------------7db1af18b064a
Content-Disposition: form-data; name="width"
21
-----------------------------7db1af18b064a--
Run Code Online (Sandbox Code Playgroud)
这些链接可以帮助您发送此数据格式,但在您的情况下,您应该避免发送文件:
http://www.groupsrv.com/dotnet/about113297.html
使用一些HTTP分析器,您可以检查发送数据这个简单的HTML代码
<html>
<body>
<form action="http://motif-x.med.harvard.edu/cgi-bin/multimotif-x.pl" enctype="multipart/form-data" method="post">
<input type="text" name="fgdata" value="SGSLDSELSVSPKRNSISRTH" /><br />
<input type="text" name="fgcentralres" value="S" /><br />
<input type="text" name="width" value="21" /><br />
<input type="submit" value="Send" />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)