C#: Http post

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajeeshun
    New Member
    • Mar 2008
    • 2

    C#: Http post

    Dear all,

    This is my first post in this forum.

    I am writing a small module for my company that send XML data to a gateway (Asp page) and get the response in XML format

    I have to send the data (request) in XML format to a web site and need to get the response for the request
    First I tried with Webclient (System.Net.Web Client) with following codes


    Code:
    Client.UploadFile("http://sample_site/upload.aspx", @"C://Regsitering_Data.xml");
    Client.DownloadFile("http://sample_site/downloaddata.aspx","C:\\Responses.asp")
    However, this way I managed to post the data. I checked with the gateway people and they confirmed that my request is correct and the webservice has sent the response correctly. However I am not able to get the response from there.

    I know that I am doing wrong. I tried with HttpWebRequest and HttpWebResponse , but I dont know how to post XML data into the with using
    HttpWebRequest

    Please help me on this. Is there any way that I can send a request to website along with XML data
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You have to do a bit more work (I think) with HttpWebRequest.
    MSDN has an example about posting data.
    You would create it, set the method type to POST, then you would need to read in the XML data into a string (I think), tell the HttpWebRequest the length of the string and then use the request stream to dump the XML data into it.

    Comment

    • rajeeshun
      New Member
      • Mar 2008
      • 2

      #3
      I have done the work. Got some samples thescripts and other sites

      Thanks to all

      Code:
       private void button2_Click(object sender, EventArgs e)
              {
                  WebRequest myRequest = null;
                  WebResponse myResponse = null;
                  
                  try
                  {
                      string Page;
                      string outputst;
                      string fileName = "C:\\Regsitering_Data.xml";
                      string uri = "https://secure.abcdef.com/xmlautomation/Upload.aspx";
                      myRequest = WebRequest.Create(uri);
                      // Post method
                      myRequest.Method = "POST";
                      // content type
                      myRequest.ContentType = "text/xml"; 
                      // Wrap the request stream with a text-based writer
                      StreamWriter writer = new StreamWriter(myRequest.GetRequestStream());
                      // Write the xml text into the stream
                      writer.WriteLine(this.GetTextFromXMLFile(fileName));
                      writer.Close();
                      // Send the data to the webserver
                      myResponse = myRequest.GetResponse();
                      Stream str = myResponse.GetResponseStream();
                      StreamReader strReader = new StreamReader(str);
                      outputst = "";
                      while ((Page = strReader.ReadLine()) != null)
                      {
                          outputst = outputst + Page;
                      }
                  }
                  catch (Exception Ex)
                  {
                      MessageBox.Show(Ex.Message.ToString());
                  }
              }
      private string GetTextFromXMLFile(string file)
              {
                  StreamReader reader = new StreamReader(file);
                  string ret = reader.ReadToEnd();
                  reader.Close();
                  return ret;
              }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        If you need more controll the
        WebRequest and WebResponse
        Can be explicitly cast as
        HttpWebRequest and HttpWebResponse

        Comment

        Working...