473,508 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#: Http post

2 New Member
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.WebClient) with following codes


Expand|Select|Wrap|Line Numbers
  1. Client.UploadFile("http://sample_site/upload.aspx", @"C://Regsitering_Data.xml");
  2. Client.DownloadFile("http://sample_site/downloaddata.aspx","C:\\Responses.asp")
  3.  
  4.  
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
Mar 19 '08 #1
3 1952
Plater
7,872 Recognized Expert Expert
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.
Mar 19 '08 #2
rajeeshun
2 New Member
I have done the work. Got some samples thescripts and other sites

Thanks to all

Expand|Select|Wrap|Line Numbers
  1.  private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             WebRequest myRequest = null;
  4.             WebResponse myResponse = null;
  5.  
  6.             try
  7.             {
  8.                 string Page;
  9.                 string outputst;
  10.                 string fileName = "C:\\Regsitering_Data.xml";
  11.                 string uri = "https://secure.abcdef.com/xmlautomation/Upload.aspx";
  12.                 myRequest = WebRequest.Create(uri);
  13.                 // Post method
  14.                 myRequest.Method = "POST";
  15.                 // content type
  16.                 myRequest.ContentType = "text/xml"; 
  17.                 // Wrap the request stream with a text-based writer
  18.                 StreamWriter writer = new StreamWriter(myRequest.GetRequestStream());
  19.                 // Write the xml text into the stream
  20.                 writer.WriteLine(this.GetTextFromXMLFile(fileName));
  21.                 writer.Close();
  22.                 // Send the data to the webserver
  23.                 myResponse = myRequest.GetResponse();
  24.                 Stream str = myResponse.GetResponseStream();
  25.                 StreamReader strReader = new StreamReader(str);
  26.                 outputst = "";
  27.                 while ((Page = strReader.ReadLine()) != null)
  28.                 {
  29.                     outputst = outputst + Page;
  30.                 }
  31.             }
  32.             catch (Exception Ex)
  33.             {
  34.                 MessageBox.Show(Ex.Message.ToString());
  35.             }
  36.         }
  37. private string GetTextFromXMLFile(string file)
  38.         {
  39.             StreamReader reader = new StreamReader(file);
  40.             string ret = reader.ReadToEnd();
  41.             reader.Close();
  42.             return ret;
  43.         }
Mar 19 '08 #3
Plater
7,872 Recognized Expert Expert
If you need more controll the
WebRequest and WebResponse
Can be explicitly cast as
HttpWebRequest and HttpWebResponse
Mar 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
17112
by: Gary Petersen | last post by:
For the benefit of others, I want to show how to do an HTTP POST request using fsockopen(). I banged my head against a wall for two days trying to figure this out. I even went to http://php.net/...
0
2428
by: Spud | last post by:
<?php // pullpage function by Nick bouton http://www.nickbouton.com/. $CustomerID = "IDHERE"; $method = "POST"; $host = "xml.mydata.com"; $usepath = "/xml.asp"; //print all vars in an...
7
2591
by: Dodger | last post by:
Apologies if the is the wrong NG. But I have trapped a GET issued by a webpage on a button press and I would like to issue the GET programatically as-and-when I wish. I wondered what the text...
0
1811
by: Owen | last post by:
Hello everyone, I am using VS.NET 2003(Trandition Chinese) Edition, and httpLook software for checking http requests. I found a problem that the following programs don't really "POST". These...
0
3860
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
5
10376
by: David Lozzi | last post by:
Howdy, I wrote a web service in .Net for my customer. My customer has another vendor who now has to consume it but they are not using Visual Studio. Most of their pages are jsp, and they said...
3
4891
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
1
2592
by: Arfeen | last post by:
Hi All, I need help again ..... I have an asp.net web page which I hit using the "HTTP POST" method. My ASP.NET page is a basic hello world example with the following code: private void...
2
5420
by: =?Utf-8?B?cGhlbmdsYWk=?= | last post by:
I am not sure really how to explain this but here goes. I am creating some new web services and I want to be able to post data like the following: <Data> <some value>1</some value> <more...
6
5093
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.