远程服务器返回错误:(500) 内部服务器错误。 C# 获取请求()
我试图自己解决这个问题,但我找不到任何有用的解决方案。我想向网站发送请求并获取处理结果。我已经遇到了这个问题(请参阅我如何填写网站表单并在 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();
这给了我以下异常:
远程服务器返回错误:(500) 内部服务器错误。
您可以采取什么措施来防止这种情况发生?
如果您想手动向站点进行输入:
数据集的文本区域:SGSLDSELSVSPKRNSISRTH
中心字符(在基本选项中):S
您将被重定向到结果的站点 - 并且可能会需要一段时间来处理。这真的是异常的原因吗?
I've tried to figure this out myself, but I can't get any useful solution. I want to send a request to a website and get the processed results. I've had a problem with this already (see How do I fill in a website form and retrieve the result in C#?) but I was able to solve it with the website stated there. Now I'm trying to access yet another website (http://motif-x.med.harvard.edu/motif-x.html) with the following code:
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();
This gives me the following exception:
The remote server returned an error: (500) Internal Server Error.
Is there anything you can do to prevent this?
In case you want to manually make an input to the site:
Text area for the data set: SGSLDSELSVSPKRNSISRTH
Central character (in basic options): S
You'll get redirected to the result's site - and it may take a while to process. Could that be actually the reason for the exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您查看文档,您可以看到对于“multipart/form-data”发送数据“POST”与“application/x-www-form-urlencoded”有很大不同。对于您的情况,您必须发送如下内容:
Content-Type:
POST:
这些链接可以帮助您发送此数据格式,但在您的情况下,您应该避免发送文件:
使用 .NET 发布多部分/表单数据WEBREQUEST
http://www.groupsrv.com/dotnet/about113297.html
使用一些 HTTP 分析器,您可以通过简单的 HTML 代码检查发送数据
If you look at the documentation you can see that for a "multipart/form-data" sending data "POST" is very different than "application/x-www-form-urlencoded". For your case, you would have to send something like this:
Content-Type:
POST:
these links can help you for sending this data format, but in your case you should avoid sending the file:
POSTING MULTIPART/FORM-DATA USING .NET WEBREQUEST
http://www.groupsrv.com/dotnet/about113297.html
With some HTTP analyzer, you can check the sending data this simple HTML code
我设置了 UserAgent 并解决了我的问题。
I set the UserAgent and my problem solved.
非常感谢您的帮助!我可以使用以下代码使其工作,以防其他人遇到此问题:
Thanks a lot for your help! I could make it work with the following code, in case anyone else encounters this problem:
我假设您正在尝试以编程方式访问motif-x。您可以使用 R 中的实现版本:
https://github.com/omarwagih/motifx
好运气。
I'm assuming you're trying to access motif-x programatically. There is an implemented version in R you can use:
https://github.com/omarwagih/motifx
Good luck.