Sending POST to a web page

O

ofiras

Hello everyone,
How can I sand a post to a web page?
I want that when the page is trying to fetch a post variable, it will
be something the program defined first.
Please help,
Ofir.
 
N

Nicholas Paldino [.NET/C# MVP]

Ofir,

Have you looked at the WebClient, or the HttpWebRequest/HttpWebResponse
methods? On all of them, you can set the Method to "POST" to post data to
the server in the request.
 
O

ofiras

Thanks a lot.
I never used net with C#, so it's new to me.
I've tried to use the WebClient class, but I didn't understand how can
I post data, so in PHP or ASP or others they can take it as a
variable.
This variable has a name and in PHP for e.g. it's $_POST['name'].
I thought the method UploadString in the WebClient class will help me.
It looks like:
UploadString(adress,method,data)
The address is the http address, the method will be post, but I didn't
understand how to determine the name of the post variable and his
contents.
And where do I get the respond? Can I get the URL and html code of the
page, which I have been directed to? Can I see it in the WebBrowser in
my form?
Thanks,
Ofir.
(Sorry if i have grammer or spelling mistakes, i don't know english
very good)
 
N

Nicholas Paldino [.NET/C# MVP]

ofiras,

What you need to do is pass the data as a browser would pass the data in
a post request. In order to do this, see this URL:

http://www.jmarshall.com/easy/http/

Specifically, look at the section "The POST Method".

The call to UploadString will return a string which is the response.

If you want more detail over the response, I would recommend using the
HttpWebRequest/HttpWebResponse directly, as it will allow you to have a
finer grained control over the request/response (everything here still
applies).
 
O

ofiras

Thenks,
Iv'e tried using sloan's HttpHelper, but i dont want to write to file,
so i just used his postDataToHttpWebRequest method.
I posted data succesfuly, but now i want to get respond...
The page i posted data to should redrect me, and i havan't seen a
change in the url adrass in the WebRequest or WebResponse (which i
made).
Is there a way to make it move like on the explorer (to be
redirected)?
If so, is there a way to show the page i get in the "webBrowser" in
the toolbox? Is there a way to get the page HTML code?
Thenks,
Ofir.
 
O

ofiras

Thanks a lot...
That was very helpful, and I did it.
Just one problem - the respond html code is partly in Hebrew, and it
shows instead of Hebrew question marks.
Is there a way to fix it?
And I want to show the html code in the WebBrowser, but when I do it,
there are a lot of "<a href='index.html'>" for e.g., same for images
and forms "action".
Is there a way to make the WebBrowser know the path so if I press a
link like this, it will direct me to the right address? (If not I
guess I'll have to check for those things and change them...)
Thanks,
Ofir.
 
O

ofiras

Thank you all, I managed to solve everything I asked here.
But after I solved everything, I have another problem:
The page I've been posting data to should make cookies in my computer,
but because I didn't send this post in explorer, it didn't.
It's a very big problem, because I need the cookies to be applied to
the "WebBrowser" tool in C# so I can show the page.
Is there a way of solving it?
Please help,
Ofir.
 
L

Liz

Thank you all, I managed to solve everything I asked here.
But after I solved everything, I have another problem:
The page I've been posting data to should make cookies in my computer,
but because I didn't send this post in explorer, it didn't.
It's a very big problem, because I need the cookies to be applied to
the "WebBrowser" tool in C# so I can show the page.
Is there a way of solving it?
Please help,
Ofir.

Sending cookies should be relatively easy from where you are now; you really
should have a look at the structure of an HTTP request header to better
understand the things you're coding here .. I don't have a good link to send
you but it shouldn't be hard to find

Receiving and persisting cookies is a different story; your browser of
course has been doing this work for you all these years but it doesn't just
happen "automatically;" you'll have to parse the HTTP response, extract the
cookies and do with them whatever needs doing

I think there's an open-source tool that will let you inspect the raw HTTP
traffic but I cannot recall the name of it ... anyone else? if it can be
unearthed one way or the other you probably want to use it so you can
demystify this process
 
C

Chris Shepherd

Liz said:
I think there's an open-source tool that will let you inspect the raw HTTP
traffic but I cannot recall the name of it ... anyone else? if it can be
unearthed one way or the other you probably want to use it so you can
demystify this process

For network traffic watching, Ethereal.
Firebug is a pretty decent browser plugin (for Firefox) as it lets you
see all elements of a request and the resultant response - and it can
even handle AJAX requests.

Chris.
 
P

Peter Duniho

For network traffic watching, Ethereal.

Noting that what was previously called Ethereal is now "Wireshark"
(some sort of trademark issue resulted in the name change when the
original Ethereal project split, or something like that).
 
O

ofiras

I didn't really understand what to do with them? Explore they're code?
Isn't there a way to load the page I send post to in the web browser
tool? I guess it's easier that transfer the cookies by myself...
I just need something that will let me to set the post variables, and
to show the page I want, with those variables, in web browser tool...
Please help,
Ofir.
 
O

ofiras

I just saw the web browser tool method overload to navigate to a URL
with post, which is:
public void Navigate (string urlString, string targetFrameName, byte[]
postData, string additionalHeaders);
So, how do I convert my NameValueCollection which I used to store the
post variables, to byte[]?
(If only I would see that method before, it would make everything a
lot easier... Next time I will know first to search also in the
method's overloads.)
Please help,
Ofir.
 
L

Liz

I just saw the web browser tool method overload to navigate to a URL
with post, which is:
public void Navigate (string urlString, string targetFrameName, byte[]
postData, string additionalHeaders);
So, how do I convert my NameValueCollection which I used to store the
post variables, to byte[]?


something like this should work (worked for me):

iterate your NameValueCollection into this form:
string postData = "var1=FirstVar&var2=SecondVar";

and use this method:

byte[] postDataBytes = Encoding.UTF8.GetBytes(postData.ToString());
 
O

ofiras

Thanks a lot.
It works :)
actually, I tried what you wrote before, but it didn't work, and I
thought that it might be because I left the Headers and the
TargetFrame null, so I searched a little on the web and found that I
need to write: "Content-Type: application/x-www-form-urlencoded" and
it worked :)
Thanks everyone here that helped me,
Ofir.
 
L

Liz

Thanks a lot.
It works :)
actually, I tried what you wrote before, but it didn't work, and I
thought that it might be because I left the Headers and the
so I searched a little on the web and found that I
need to write: "Content-Type: application/x-www-form-urlencoded" and
it worked :)

my code does not send this header and works fine ... but if yours works, it
works
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top