views:

1031

answers:

3

First of all, I am a WEB NOOB. Which probably explains this question. Anyway, when I use the web page test app to post xml to a url, everything works fine. Here is the pertinant code from the web page (i think):

<form action="/gateway/xml" method="post" id="xml-test-form">
  <textarea name="data">

  {some xml is in here}

  </textarea>
  <input type="submit" value="Submit Test" />
</form>

When I try to submit the exact same XML using C# (WebRequest or HttpWebRequest) with content type of ("text/xml" or "application/x-www-form-urlencoded") with a buffer encoded (ASCII or UTF8) I get an error that implies the XML cant be read at all on the other end. Here is the error:

<?xml version="1.0"?>
<SubmitOrdersResponse>
  <status code="0">FAILURE</status>
  <errors>
    <error code="1001">Invalid XML Version</error>
  </errors>
</SubmitOrdersResponse>
<br /><b>Warning</b>:  DOMDocument::loadXML() [<a href='domdocument.loadxml'>domdocument.loadxml</a>]: Empty string supplied as input in <b>/var/www/vhosts/reports.gogetagrip.com/httpdocs/application/models/Gateway.php</b> on line <b>90</b><br />

I can reproduce this error using the web tester by removing a XML element named . I think this is the first element that is checked for, and hence the "INVALID XML VERSION" error. I think what is happening is my submittal is comming accross slighlty in the wrong format and that element can't be read. I think specifically I have to simulate a posting data where my data is comming from the "data" form field (see above). I don't know how to set that using the WebRequest class, so I can't test it. Here is my code:

static private void Post(string sURL, string sXml) {
  try {
    //Our postvars
    byte[] buffer = Encoding.UTF8.GetBytes(sXml);  // Tried ASCII...same result
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(sURL);  // Tried WebRequest ... same result
    WebReq.Method = "POST";
    WebReq.ContentType = "application/x-www-form-urlencoded";  // tried "text/xml"... same result
    WebReq.ContentLength = buffer.Length;
    Stream ReqStream = WebReq.GetRequestStream();
    ReqStream.Write(buffer, 0, buffer.Length);
    ReqStream.Close();
    WebResponse WebRes = WebReq.GetResponse();
    //Console.WriteLine(WebResp.StatusCode);
    //Console.WriteLine(WebResp.Server);
    Stream ResStream = WebRes.GetResponseStream();
    StreamReader ResReader = new StreamReader(ResStream);
    string sResponse = ResReader.ReadToEnd();
  } catch (Exception ex) {

  } finally {

  }
}

Any Ideas?????

A: 

try adding true to the StreamReader initialisation.

StreamReader ResReader = new StreamReader(ResStream, true)

It will detect Byte Order Mark then.

Kindness,

Dan

Daniel Elliott
Thanks! However, I can read the response fine, its the submit I am screwing up.
johnnycrash
+2  A: 

Yes, there is an easier way to do a Forms submit using System.Net... The WebClient class is your friend.

In this case you would use webClient::UploadValues() and it will take care of everything else.

feroze
A: 

I am facing the same problem with the same schenario. any body knows solution. I had tried webclient also

kalpesh