473,775 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with WebRequest and POST

I am trying to submit data to a webpage in the form of a post and my code is
below. It is a function that takes a url and the post content as strings
and then performs the post. But as soon as I add the post data to the
headers I get an exception that just says "headers". Can anyone please help
me here? Thanks....

public string Get(string u, string c)
{
WebRequest wr = WebRequest.Crea te(u);
wr.Headers.Add( c);
wr.Method="POST ";
wr.ContentType = "text/xml";
wr.ContentLengt h = c.Length;
wr.Timeout = 5000;

StreamReader sr = new StreamReader(wr .GetResponse(). GetResponseStre am());
string response = sr.ReadToEnd();
sr.Close();
return response;
}

Keith
Nov 15 '05 #1
2 3277
"Keith Selbee" wrote:
I am trying to submit data to a webpage in the form of a post and my
code is below. It is a function that takes a url and the post
content as strings and then performs the post. But as soon as I add
the post data to the headers I get an exception that just says
"headers". Can anyone please help me here? Thanks....

public string Get(string u, string c)
{
WebRequest wr = WebRequest.Crea te(u);
wr.Headers.Add( c);
wr.Method="POST ";
wr.ContentType = "text/xml";
wr.ContentLengt h = c.Length;
wr.Timeout = 5000;

StreamReader sr = new
StreamReader(wr .GetResponse(). GetResponseStre am()); string response
= sr.ReadToEnd(); sr.Close();
return response;
}


You're posting an empty body and the wrong content length. I'm sure
that's not what you want to do ;-)

Assuming "c" is your POST data, it should be written to the request
stream, not put into the headers collection.

Cheers,
--
Joerg Jooss
jo*********@gmx .net
Nov 15 '05 #2
You don't add the post data to the headers. You add it to the body. I
attached sample code.
Hope this helps,

Randy
http://www.kbcafe.com

"Keith Selbee" <ks*****@nospam .neo.rr.com> wrote in message news:<eq******* *******@TK2MSFT NGP12.phx.gbl>. ..
I am trying to submit data to a webpage in the form of a post and my code is
below. It is a function that takes a url and the post content as strings
and then performs the post. But as soon as I add the post data to the
headers I get an exception that just says "headers". Can anyone please help
me here? Thanks....

/*************** *************** *************** ********
* writeToURL is a method that writes the contents of
* a specified URL to the web
*************** *************** *************** ********/
void writeToURL (WebRequest request, string data)
{

byte [] bytes = null;
// Get the data that is being posted (or sent) to the server
bytes = System.Text.Enc oding.ASCII.Get Bytes (data);
request.Content Length = bytes.Length;
// 1. Get an output stream from the request object
Stream outputStream = request.GetRequ estStream ();

// 2. Post the data out to the stream
outputStream.Wr ite (bytes, 0, bytes.Length);

// 3. Close the output stream and send the data out to the web server
outputStream.Cl ose ();
} // end writeToURL method

/*************** *************** *************** ********
* retrieveFromURL is a method that retrieves the contents of
* a specified URL in response to a request
*************** *************** *************** ********/
String retrieveFromURL (WebRequest request)
{
// 1. Get the Web Response Object from the request
WebResponse response = request.GetResp onse();
// 2. Get the Stream Object from the response
Stream responseStream = response.GetRes ponseStream();

// 3. Create a stream reader and associate it with the stream object
StreamReader reader = new StreamReader (responseStream );

// 4. read the entire stream
return reader.ReadToEn d ();
}// end retrieveFromURL method

/*************** *************** *************** ********
* postToURL is a method that forces a POST
* of data to a specified URL
*
* A HTTP POST is a combination of a write to the Web Server
* and an immediate read from the Web server
*************** *************** *************** ********/
void postToURL (string url, string data)
{

// Create the Web Request Object
WebRequest request = WebRequest.Crea te(url);
// Specify that you want to POST data
request.Method = "POST";
request.Content Type = "applicatio n/x-www-form-urlencoded";
if (data != null)
{
// write out the data to the web server
writeToURL (request, data);
}
else
{
request.Content Length = 0;
}
// read the response from the Web Server
string htmlContent = retrieveFromURL (request);
} // end postToURL method
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6351
by: Paul | last post by:
Hello, First I want to refer to the problem "WebRequest : execute a button" of a few days ago. The way I solved it, I loose my session, and as a consequence my session variables. I don't want to keep those variables, as an alternative, as ViewState variables because I don't want to transfer to many hidden fields. This is the code I use :
1
2491
by: Juan Dent | last post by:
Hi, The Request.SaveAs method stores in a file the complete HTTP request including the HttpVersion. However when I try to reproduce in memory the same thing, I cannot find the "HTTP/1.1" anywhere. I looked around and found a class called HttpWorkerRequest which has a method called GetHttpVersion() which returns the exact thing I am missing. However, I can't see how to get at the HttpWorkerRequest from a Page or from the HttpApplication.
2
2271
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html form in c# code: <form method="post" enctype="multipart/form-data" action="https://www.site.com/upload/"/> <input type="file" name="xml" id="xml="><br>
3
2807
by: Stephane | last post by:
Hi, I'm trying to use PayPal and its Instant Payment Notification. In short, when a payment is made, PayPal send a post to my server and I post it back to PayPal. I'm using WebRequest to do this. I receive the PayPal post, but I can't post it back. It's always giving me a Time out. Here's my code:
0
2858
by: MARTIN LANNY | last post by:
Hi everyone, To bring you in the picture: I have the function to grab the html content from the page. Function requires only URL and Time Delay variables. Url, which I am submitting to this function, includes many variables, such as name and password and others... Problem started when website I am logging to, changed the method of logging from GET to POST.
0
1188
by: Glenn Venzke | last post by:
I am trying to invoke a web service from an ASP.net page using the WebRequest object to an HTTP post. The web service is basically a print manager that prints sets of documents based an an invoice number passed as an argument. The first time I invoke it, it works fine. However when I do a second invokation, the http request hangs until it times out. When I tell to time out at 30 minutes, it hangs for those 30 minutes, then times out. I...
29
2152
by: jens Jensen | last post by:
Hello, I got this "breath taking" task to write a an http server to which "xml data" will be posted to and will answer with xml data. The logic behind the xml processing is not a matter here. My question is : Can i configure a webservice for HHTP POST ? The remote peer just performs an http web request. NO RPC style call to my system.
1
8206
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie to SMCHALLANGE=YES to allow me to obtain a session. I then iterate the cookie collection to extract the SMSESSION value. All is good (so far). This is when the sky turns grey and the rain starts to fall. I then use this SMSESSION in a web...
5
4456
by: jitsu | last post by:
Hi all, I have a problem. I need to connect to Web Server, send a xml string (query) to it and receive another xml string (result). I have the location of the Web Server like: https://extranet....com:8443, then user name and password, then I have the certifiace (I have already installed it to my computer) and I know the xml query string like:
0
9622
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6718
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.