how can I change this get code to post code

C

cj

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim URL As String
Dim webReq As Net.HttpWebRequest
Dim webResp As Net.HttpWebResponse
Dim respStr As String

Try
URL = "http://foo.com/Test.dll?user=sam&field1=&field2=testing"

webReq = Net.HttpWebRequest.Create(URL)
webReq.Timeout = 5000
webResp = webReq.GetResponse
respStr = New
IO.StreamReader(webResp.GetResponseStream).ReadToEnd
Catch ex As Exception
MessageBox.Show("ex.Message = " & ex.Message)
Finally
webResp.Close()
End Try

TextBox1.Text = respStr
End Sub


I someone would convert this code to use post as an example for me I'd
appreciate it. Note field1 is supposed to be empty.

Thanks!
 
S

Steven Cheng[MSFT]

Hi Cj,

As for the "how to change code to post code" question, do you mean you want
to make the httpwebrequest component send a http"POST" message to target
server endpoint?

If this is the case, you can simply set HttpWebRequest.Method property to
"POST"

#HttpWebRequest.Method Property
https://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.method.a
spx

and here is a simple test method which use webrequest to send HTTP POST
request to the website(without any data in request message):
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim req As HttpWebRequest
Dim rep As HttpWebResponse
Dim sr As StreamReader
Dim strContent As String


Dim url As String

url = "http://www.asp.net"

req = WebRequest.Create(url)

req.Method = "POST"
req.ContentLength = 0


rep = req.GetResponse()

sr = New StreamReader(rep.GetResponseStream())

strContent = sr.ReadToEnd()

sr.Close()
rep.Close()


Me.TextBox1.Text = strContent


End Sub
here are some web articles introducing posting data with webrequest and
have exmaples on add some parameters (if you want to send some data with
the post request)

#POSTing Data with ASP.NET
http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp

#Create dynamic data and post to a server
http://google.com/answers/threadview?id=174465

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
C

Cor Ligthert[MVP]

Steven,

Well done by changing the URL, these kinds of invitations are giving me
always some strange ideas. As Herfried made me yesterday attend on is it
probably better next time to remove the part with the original URL in the
text before you send.

I am not saying anything about cj, only that it gives me strange ideas to
start an Internet procedure.

:)

Cor
 
C

cj

I'm sorry I'm still a bit confused. I understand from your reply that I
need to add .method = "POST" and .ContentLength = to my code like below.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim URL As String
Dim webReq As Net.HttpWebRequest
Dim webResp As Net.HttpWebResponse
Dim respStr As String

Try
URL = "http://foo.com/Test.dll?user=sam&field1=&field2=testing"

webReq = Net.HttpWebRequest.Create(URL)
webReq.Timeout = 5000
webreq.method = "POST"
webreq.ContentLength = 0
webResp = webReq.GetResponse
respStr = New
IO.StreamReader(webResp.GetResponseStream).ReadToEnd
Catch ex As Exception
MessageBox.Show("ex.Message = " & ex.Message)
Finally
webResp.Close()
End Try

TextBox1.Text = respStr
End Sub

1. I don't think ?user=sam&field1=&field2=testing should remain in the
URL and you didn't indicate where to put it?

2. ContenLength shouldn't be 0 should it? I think it should be 31 in
my case. Right?

Also, just FYI this is not an ASP program. It's a windows program that
just happens to need to contact a web site for info.

Thanks
 
C

cj

Steven, I don't understand what you are trying to say. I made up
"http://foo.com/Test.dll?user=sam&field1=&field2=testing" just for this
posting. I wouldn't post a real address as I don't want to give out our
company's site info. So don't worry about me posting foo.com, to my
knowledge it doesn't exist and the rest of the string is also just examples.

If I can understand how to convert what we had been doing with a get as
shown by my example into post I can convert my live application.
 
S

Steven Cheng[MSFT]

Thanks for your reply Cj,

First, I have never added comments against the url you added, I think maybe
Cor Ligthert have some suggestion on this(not Steven Cheng).

Second, about the test code snippet I posted, I've mentioned that it is a
simple test which post no data(no name&value pairs. And in the article I
pasted eariler, there are complete code snippets about add name & value
pairs. I don't think you've ever read any of the articles I've provided.
BTW, if you do have complain on those articles or on me, please feel free
to let me know, then, I should not paste them next time.

Well, for your scenario, if you want to translate the full url (GET method)
to POST one:

"http://foo.com/Test.jsp?user=sam&field1=&field2=testing"

you can use the following code:

This time, I used the exact url you've posted, you can freely to change it
(no matter your target url is ".dll" or ".jsp")Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim req As HttpWebRequest
Dim rep As HttpWebResponse
Dim sr As StreamReader
Dim strContent As String
Dim postData As String
Dim bytes() As Byte
Dim sw As StreamWriter


Dim url As String


url = "http://localhost:58549/post_handler.ashx"
postData = "user=sam&field1=&field2=testing"


req = WebRequest.Create(url)

req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"

sw = New StreamWriter(req.GetRequestStream(), Encoding.UTF8)
sw.Write(postData)
sw.Close()


rep = req.GetResponse()

sr = New StreamReader(rep.GetResponseStream())

strContent = sr.ReadToEnd()

sr.Close()
rep.Close()

Me.TextBox1.Text = strContent
End Sub

<<<<<<<<<<<<<<<<<<<<<<

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
Date: Thu, 30 Aug 2007 10:21:39 -0400
 
C

cj

Thank you Steven

First, Yes it was Cor that said something about the url that I take it
meant I shouldn't have posted it. Sorry I got confused on the replies.

Second, I did read your post and article several times and browsed
through other parts of the MSDN site you pointed me to but apparently
I'm not quite as up to speed on this subject as many of you are and I
still couldn't figure out what to change in my program. As for you not
sending data I didn't know what you meant by "without any data in
request message". It never crossed my mind that you could post with
sending data. I can't imagine why such a thing would be done.

Please don't get upset. Just so you understand when I look at the code I
sent you and then at your code I see you have much more code than I do.
When I start going through it I find that you changed my variable
names--for instance webresp to rep--and that you've broken out the
streamreader code. Where I had

respstr = new io.streamreader(webresp.getresponsestream).readtoend

you have

dim sr as streamreader
dim strcontent as string
sr=new streamreader(rep.getresponsestream())
strcontent=sr.readtoend()

For you this might be easy but for me all this makes it hard for me to
quickly say, oh, the new parts of the code I needed are

webreq.contenttype="application/x-www-form-urlencoded"

then sw=new streamwriter(req.getrequeststream(), encoding.utf8)
sw.write(postdata)
sw.close


Now to understand better the changes I need to make. This might be
obvious to you but it isn't to me so let me make sure I understand what
we are doing. In a get I didn't write anything but in a post I have to
so that is why the streamwriter is required. Makes sense I hope I'm
right. So sw=new streamwriter(req.getrequeststream(), encoding.utf8) is
creating a streamwriter with the output of it being
req.getrequeststream. Then sw.write(postdata) is putting the postdata
into the req. But nothing has actually been sent to the jsp page yet.
Right? Now when we run rep=req.getresponse() the postdata that was put
in the web request req is now being sent to the jsp page and it's reply
is being stored in rep. We are now ready to read the response from rep
etc. Do I understand this process correctly?

I know it's hard typing all this back in forth. It would have taken 5
minutes if you was standing here but I don't have anyone that I can grab
like that for a 5 minute discussion. I appreciate your help.

One last thing. Why do you use the url
http://localhost:58549/post_handler.ashx ? Is this some kind of loop
back that I can use for testing? I've never heard of an ashx file.

Ok I'm going to send this now and then I'm going to play with the code
some more and see if I can get it working.



Thanks for your reply Cj,

First, I have never added comments against the url you added, I think maybe
Cor Ligthert have some suggestion on this(not Steven Cheng).

Second, about the test code snippet I posted, I've mentioned that it is a
simple test which post no data(no name&value pairs. And in the article I
pasted eariler, there are complete code snippets about add name & value
pairs. I don't think you've ever read any of the articles I've provided.
BTW, if you do have complain on those articles or on me, please feel free
to let me know, then, I should not paste them next time.

Well, for your scenario, if you want to translate the full url (GET method)
to POST one:

"http://foo.com/Test.jsp?user=sam&field1=&field2=testing"

you can use the following code:

This time, I used the exact url you've posted, you can freely to change it
(no matter your target url is ".dll" or ".jsp")
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim req As HttpWebRequest
Dim rep As HttpWebResponse
Dim sr As StreamReader
Dim strContent As String
Dim postData As String
Dim bytes() As Byte
Dim sw As StreamWriter


Dim url As String


url = "http://localhost:58549/post_handler.ashx"
postData = "user=sam&field1=&field2=testing"


req = WebRequest.Create(url)

req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"

sw = New StreamWriter(req.GetRequestStream(), Encoding.UTF8)
sw.Write(postData)
sw.Close()


rep = req.GetResponse()

sr = New StreamReader(rep.GetResponseStream())

strContent = sr.ReadToEnd()

sr.Close()
rep.Close()

Me.TextBox1.Text = strContent
End Sub

<<<<<<<<<<<<<<<<<<<<<<

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
Date: Thu, 30 Aug 2007 10:21:39 -0400
Steven, I don't understand what you are trying to say. I made up
"http://foo.com/Test.dll?user=sam&field1=&field2=testing" just for this
posting. I wouldn't post a real address as I don't want to give out our
company's site info. So don't worry about me posting foo.com, to my
knowledge it doesn't exist and the rest of the string is also just examples.
If I can understand how to convert what we had been doing with a get as
shown by my example into post I can convert my live application.

Steven,

Well done by changing the URL, these kinds of invitations are giving me
always some strange ideas. As Herfried made me yesterday attend on is it
probably better next time to remove the part with the original URL in
the text before you send.

I am not saying anything about cj, only that it gives me strange ideas
to start an Internet procedure.

:)

Cor

"Steven Cheng[MSFT]" <[email protected]> schreef in bericht
Hi Cj,

As for the "how to change code to post code" question, do you mean you
want
to make the httpwebrequest component send a http"POST" message to target
server endpoint?

If this is the case, you can simply set HttpWebRequest.Method property to
"POST"

#HttpWebRequest.Method Property
https://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.method.a
spx

and here is a simple test method which use webrequest to send HTTP POST
request to the website(without any data in request message):

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim req As HttpWebRequest
Dim rep As HttpWebResponse
Dim sr As StreamReader
Dim strContent As String


Dim url As String

url = "http://www.asp.net"

req = WebRequest.Create(url)

req.Method = "POST"
req.ContentLength = 0


rep = req.GetResponse()

sr = New StreamReader(rep.GetResponseStream())

strContent = sr.ReadToEnd()

sr.Close()
rep.Close()


Me.TextBox1.Text = strContent


End Sub
here are some web articles introducing posting data with webrequest and
have exmaples on add some parameters (if you want to send some data with
the post request)

#POSTing Data with ASP.NET
http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp

#Create dynamic data and post to a server
http://google.com/answers/threadview?id=174465

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 
S

Steven Cheng[MSFT]

Thanks for your reply Cj,

Let's focus on the process you mentioned here first:
================
Now to understand better the changes I need to make. This might be
obvious to you but it isn't to me so let me make sure I understand what
we are doing. In a get I didn't write anything but in a post I have to
so that is why the streamwriter is required. Makes sense I hope I'm
right. So sw=new streamwriter(req.getrequeststream(), encoding.utf8) is
creating a streamwriter with the output of it being
req.getrequeststream. Then sw.write(postdata) is putting the postdata
into the req. But nothing has actually been sent to the jsp page yet.
Right? Now when we run rep=req.getresponse() the postdata that was put
in the web request req is now being sent to the jsp page and it's reply
is being stored in rep. We are now ready to read the response from rep
etc. Do I understand this process correctly?
==============

Yes, you've got them correctly. Let me further summary it for your
reference:

** For HTTP GET method, you do not send any data in HTTP request's message
body, but only append some simple text data(name=value pairs) in the url
querystring ( http://?param1=value1&param2=value2......)...nline Support Lead [/QUOTE] [/QUOTE] [/QUOTE]
 
C

cj

Thanks Steven, I think I'm good for now. Priorities have changed again
here and I'm now on something else but I'm sure I'll be back to this
again later on.

cj


Thanks for your reply Cj,

Let's focus on the process you mentioned here first:
================
Now to understand better the changes I need to make. This might be
obvious to you but it isn't to me so let me make sure I understand what
we are doing. In a get I didn't write anything but in a post I have to
so that is why the streamwriter is required. Makes sense I hope I'm
right. So sw=new streamwriter(req.getrequeststream(), encoding.utf8) is
creating a streamwriter with the output of it being
req.getrequeststream. Then sw.write(postdata) is putting the postdata
into the req. But nothing has actually been sent to the jsp page yet.
Right? Now when we run rep=req.getresponse() the postdata that was put
in the web request req is now being sent to the jsp page and it's reply
is being stored in rep. We are now ready to read the response from rep
etc. Do I understand this process correctly?
==============

Yes, you've got them correctly. Let me further summary it for your
reference:

** For HTTP GET method, you do not send any data in HTTP request's message
body, but only append some simple text data(name=value pairs) in the url
querystring ( http://?param1=value1&param2=value2......)...Online Support Lead [/QUOTE][/QUOTE] [/QUOTE]
 
S

Steven Cheng[MSFT]

No problem. Welcome to post here if you have new questions or anything need
help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
Date: Tue, 04 Sep 2007 15:41:23 -0400
From: cj <[email protected]>
Subject: Re: how can I change this get code to post code
Thanks Steven, I think I'm good for now. Priorities have changed again
here and I'm now on something else but I'm sure I'll be back to this
again later on.

cj


Thanks for your reply Cj,

Let's focus on the process you mentioned here first:
================
Now to understand better the changes I need to make. This might be
obvious to you but it isn't to me so let me make sure I understand what
we are doing. In a get I didn't write anything but in a post I have to
so that is why the streamwriter is required. Makes sense I hope I'm
right. So sw=new streamwriter(req.getrequeststream(), encoding.utf8) is
creating a streamwriter with the output of it being
req.getrequeststream. Then sw.write(postdata) is putting the postdata
into the req. But nothing has actually been sent to the jsp page yet.
Right? Now when we run rep=req.getresponse() the postdata that was put
in the web request req is now being sent to the jsp page and it's reply
is being stored in rep. We are now ready to read the response from rep
etc. Do I understand this process correctly?
==============

Yes, you've got them correctly. Let me further summary it for your
reference:

** For HTTP GET method, you do not send any data in HTTP request's message
body, but only append some simple text data(name=value pairs) in the url
querystring ( http://?param1=value1&param2=value2......)...nline Support Lead [/QUOTE] [/QUOTE] [/QUOTE]
 

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