Domanda

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?

È stato utile?

Soluzione

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>

Altri suggerimenti

I set the UserAgent and my problem solved.

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

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top