views:

34

answers:

1

Hi to all

I have a some problem while Posting xml data Using HttpWebRequest. Actually I want to Post 3 Form variables

2 of them are for credential and the third one is for XML data to an api, The api will authenticate and will process the xml data, and will return success if no error found.

Here is what there documentation says.

The data will be passed to the gateway via an HTTPS FORM post and a string value of "success" will be returned upon successful receipt of the data. Three total FORM variables will be posted, two of which will contain credentials, and the third will contain the HR-XML data. A string value of "error" will be returned if the posting fails for any reason.

Form Fields integration_field1 = 1234, integration_field2=2345pwd, hrxml=form field containing the HR-XML order (string)

Here is what I have Coded..

string strId = "123";
        string strName = "test";
        StreamReader sr2 = File.OpenText(Server.MapPath("~/App_Data/XMLFile.xml"));
        string xml = sr2.ReadToEnd();
        sr2.Close();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        xml = HttpUtility.UrlEncode(xmlDoc.OuterXml);

        System.Text.UTF8Encoding encoding = new UTF8Encoding();
        string postData = "xmldata=" + xml;

        byte[] data = encoding.GetBytes(postData);

        // Prepare web request...
        HttpWebRequest myRequest =
          (HttpWebRequest)WebRequest.Create("http://localhost:1994/TestXMLPost/PostData.aspx");
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        Stream newStream = myRequest.GetRequestStream();
        // Send the data.
        newStream.Write(data, 0, data.Length);

        newStream.Close();

        WebResponse res = (HttpWebResponse)myRequest.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream());
        string str = sr.ReadToEnd();
        sr.Close();

On PostData.aspx, I have done this for testing purpose

if (Request.Form.Count > 0)
     {
       Response.Write( Request.Form["xmldata"] + "");

      }

I did not received any thing in the response. Only the error "The remote server returned an error: (500) Internal Server Error." Here is the HTML form which i want to simulate Using HttpWebRequest.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>testing</title>
</head>

<body>
<form action="https://myurl.com/samplepage.cfm" method="post" >
integration_id<input type="text" name="integration_id" value="id" /><br />
integration_pwd<input type="text" name="integration_pwd" value="pwd"  /><br />
<textarea name="hrxml" style="height: 404px; width: 593px">
<?xml version="1.0" encoding="UTF-8"?>
<BackgroundCheck account="test" userId="test" password="test">
  <BackgroundSearchPackage>
    <ReferenceId>
      <IdValue>12345678</IdValue>
    </ReferenceId>
    <PersonalData>
      <PersonName>
        <GivenName>TEST</GivenName>
        <FamilyName primary="undefined">TEST</FamilyName>
      </PersonName>
      <DemographicDetail>
        <GovernmentID countryCode="US" issuingAuthority="SSN">00000000000000</GovernmentID>
        <DateOfBirth>1901-01-01</DateOfBirth>
      </DemographicDetail>
    </PersonalData>
  </BackgroundSearchPackage>
</BackgroundCheck>
</textarea>
<input type="submit" />

</form>
</body>
</html>

Thanks

A: 

Use fiddler to see what's actually going over the wire.

Alternatively write a copy of what you are sending to disk.

Besides the above, you could also check the event viewer of the target computer for an entry related to the error (if you don't have any alternate logging of errors in place).

Bottom line is that you need to see what's going on.

eglasius
Thank for your suggestion. It really help me. I went to see the Event Viewer and surprised to see that the Page where xml data is post, Complains about the request for Potential dangerous request. And we all knows that Asp.net page validate each incoming request. So when I set Page attribute ValidateRequest="false". The Problem is now resolved. Thanks budy...:)
Jehanzeb afridi
you welcome, please accept the answer :)
eglasius