简体   繁体   English

远程服务器返回错误:(500)Internal Server Error。 GetRequest()C#

[英]The remote server returned an error: (500) Internal Server Error. GetRequest() C#

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. 我已经遇到了问题(请参阅如何填写网站表单并使用C#检索结果? ),但是我能够使用此处说明的网站来解决此问题。 Now I'm trying to access yet another website ( http://motif-x.med.harvard.edu/motif-x.html ) with the following code: 现在,我尝试使用以下代码访问另一个网站( 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();

This gives me the following exception: 这给了我以下例外:

The remote server returned an error: (500) Internal Server 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 数据集的文本区域: SGSLDSELSVSPKRNSISRTH

Central character (in basic options): S 中心字符(在基本选项中):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? 那真的是例外的原因吗?

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". 如果查看文档,您会发现对于“ multipart / form-data”,发送数据“ POST”与“ 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: 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 使用.NET WEBREQUEST发布多部分/表单数据

http://www.groupsrv.com/dotnet/about113297.html http://www.groupsrv.com/dotnet/about113297.html

With some HTTP analyzer, you can check the sending data this simple HTML code 使用一些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>

我设置了UserAgent,问题解决了。

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

I'm assuming you're trying to access motif-x programatically. 我假设您正在尝试以编程方式访问motif-x。 There is an implemented version in R you can use: R中有一个已实现的版本,您可以使用:

https://github.com/omarwagih/motifx https://github.com/omarwagih/motifx

Good luck. 祝好运。

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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程服务器返回错误:(500)Internal Server Error。 - The remote server returned an error: (500) Internal Server Error. 使用c#将图像发布到Web服务器(远程服务器返回错误(500)内部服务器错误) - Post Image to Web Server in c# (The remote Server returned an error (500) Internal Server Error) System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。 - System.Net.WebException: The remote server returned an error: (500) Internal Server Error. Web API:远程服务器返回错误。 (500内部服务器错误。 仅在联机时,不在本地计算机中 - Web API : The remote server returned an error. (500) Internal Server Error. Only when online and not in Local Machine 从Windows服务将数据发布到Web应用程序时出错。“远程服务器返回错误:(500)内部服务器错误。” - Error While posting Data to a web application from a windows service.“The remote server returned an error: (500) Internal Server Error.” 远程服务器返回错误:(500)内部服务器错误Web服务 - The remote server returned an error: (500) Internal Server Error Web service HttpWebRequest的。 远程服务器返回错误:(500)内部服务器错误 - HttpWebRequest. The remote server returned an error: (500) Internal Server Error webclient远程服务器返回错误:(500)内部服务器错误 - webclient The remote server returned an error: (500) Internal Server Error Mandrill : 远程服务器返回错误:(500) 内部服务器错误 - Mandrill : The remote server returned an error: (500) internal server error 远程服务器返回错误:(500)网站上的内部服务器错误 - The remote server returned an error: (500) Internal Server Error on website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM