-3
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>NOVA</title>
</head>
<body>
<form method="post" action="file.com/">
<table>
  <tr>
    <td>dataType</td>
    <td><input type="text" name="dataType" value="xml"></td>
  </tr>
  <tr>
    <td>actionType</td>
    <td><input type="text" name="actionType" value="query"></td>
  </tr>
  <tr>
    <td>accountid</td>
    <td><input type="text" name="accountid" value="121758"></td>
  </tr>
  <tr>
    <td>checksum</td>
    <td><input type="text" name="file" value="123"></td>
  </tr>
  <tr>
    <td></td>
    <td style="text-align:right"><input type="submit" value="send"></td>
  </tr>
</table>
</form>
</body>
</html>

This is what I've tried.

string xml = @"<Array><dataType>xml</dataType><actionType>query</actionType><accountid>121758</accountid><file>" + "123" + "</file></Array>";
WebRequest req = null;
WebResponse rsp = null;

string uri = "https://file.com/";
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.WriteLine(xml);
writer.Close();
rsp = req.GetResponse();

but this is not the same. I want exactly the same as form.submit();

levi
  • 3,451
  • 6
  • 50
  • 86
  • 1
    _"This is what I've tried."_ - so, what happened? What do you expect to happen? What have you tried exactly, apart from guessing some XML format that is not being sent by the browser? Please take a look at [HTTP request with post](http://stackoverflow.com/questions/4015324/http-request-with-post). – CodeCaster Dec 07 '12 at 13:47
  • 1
    What do you expect as my answer for you question?.. It is not the same as submitting the form!!! Read the question's title – levi Dec 07 '12 at 13:57
  • I expect you to show what you want to happen. You can't just dump your code and say _"This don't work"_. If you take a look using [Fiddler](http://www.fiddler2.com/fiddler2/) for example, you'll see your form gets posted with `dataType=xml&actionType=query&accountid=121758&file=123`, not some arbitrary XML format. Calling `form.submit()` from JavaScript won't change this. So I guess the accepted answer doesn't do what you say you want it to do, it doesn't do the same as `form.submit()` but it simply posts `dataType=xml`. – CodeCaster Dec 07 '12 at 14:15

1 Answers1

2

You're code is more complicated than needed. It's also incorrect. For example, the ContentType should be multipart/form-data, not text/xml.

Instead, use a WebClient in conjunction with a NameValueCollection:

using(WebClient client = new WebClient()) {
    NameValueCollection formValues = new NameValueCollection();
    formValues.Add("dataType", "xml");
    client.UploadValues("http://www.somesite.com/SomePage.aspx", formValues);
}

If you need to capture the response as well, use this method:

byte[] response = null;

using(WebClient client = new WebClient()) {
    NameValueCollection formValues = new NameValueCollection();
    formValues.Add("dataType", "xml");
    response = client.UploadValues("http://www.somesite.com/SomePage.aspx", formValues);
}
James Hill
  • 60,353
  • 20
  • 145
  • 161