远程服务器返回错误:(500) 内部服务器错误。 C# 获取请求()

发布于 2024-12-02 08:56:28 字数 1432 浏览 1 评论 0原文

我试图自己解决这个问题,但我找不到任何有用的解决方案。我想向网站发送请求并获取处理结果。我已经遇到了这个问题(请参阅我如何填写网站表单并在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凤舞天涯 2024-12-09 08:56:28

如果您查看文档,您可以看到对于“multipart/form-data”发送数据“POST”与“application/x-www-form-urlencoded”有很大不同。对于您的情况,您必须发送如下内容:

Content-Type:

Content-Type: multipart/form-data; boundary=---------------------------7db1af18b064a

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--

这些链接可以帮助您发送此数据格式,但在您的情况下,您应该避免发送文件:

使用 .NET 发布多部分/表单数据WEBREQUEST

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>

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:

Content-Type: multipart/form-data; boundary=---------------------------7db1af18b064a

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--

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

<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>
醉殇 2024-12-09 08:56:28

我设置了 UserAgent 并解决了我的问题。

 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";

I set the UserAgent and my problem solved.

 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
生死何惧 2024-12-09 08:56:28

非常感谢您的帮助!我可以使用以下代码使其工作,以防其他人遇到此问题:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                        "http://motif-x.med.harvard.edu/cgi-bin/multimotif-x.pl");
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
string boundary = "---------------------------7db1af18b064a";
string newLine = "\r\n";
string postData = "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"fgdata\"" + newLine + newLine + "SGSLDSELSVSPKRNSISRTH" + newLine +
                  "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"fgcentralres\"" + newLine + newLine + "S" + newLine +
                  "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"width\"" + newLine + newLine + "21" + newLine +
                  "--" + boundary + "--";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

using (WebResponse response = request.GetResponse())
using (Stream resSteam = response.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
    File.WriteAllText("SearchResults.html", sr.ReadToEnd());
System.Diagnostics.Process.Start("SearchResults.html");

Thanks a lot for your help! I could make it work with the following code, in case anyone else encounters this problem:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                        "http://motif-x.med.harvard.edu/cgi-bin/multimotif-x.pl");
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
string boundary = "---------------------------7db1af18b064a";
string newLine = "\r\n";
string postData = "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"fgdata\"" + newLine + newLine + "SGSLDSELSVSPKRNSISRTH" + newLine +
                  "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"fgcentralres\"" + newLine + newLine + "S" + newLine +
                  "--" + boundary + newLine +
                  "Content-Disposition: form-data; name=\"width\"" + newLine + newLine + "21" + newLine +
                  "--" + boundary + "--";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

using (WebResponse response = request.GetResponse())
using (Stream resSteam = response.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
    File.WriteAllText("SearchResults.html", sr.ReadToEnd());
System.Diagnostics.Process.Start("SearchResults.html");
浅沫记忆 2024-12-09 08:56:28

我假设您正在尝试以编程方式访问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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文