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

Anna picture Anna · Sep 1, 2011 · Viewed 14.6k times · Source

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?

Answer

andres descalzo picture andres descalzo · Sep 1, 2011

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>