Re: how can I change this get code to post code
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Mon, 03 Sep 2007 02:47:44 GMT
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://[url]?param1=value1¶m2=value2......). And when
you use httpWebRequest to send GET request, you do not need to use
StreamWriter to open the "RequestStream" or write data into it(just append
them in url is enough)
** For HTTP POST method, you do not need to use url querystring for
additional data. You can use the HTTP message body. And in .net
HttpWebRequest class, you simpley open the "RequestStream" and write the
data into it and close it(before you send the request)
** When you write the data into WebRequest's "RequestStream" and close it,
the data(even the request) still hasn't been send. It is only when you call
"WebRequest.GetResponse" method that the actually http communication
between client and server starts. Also, after the GetResponse method ends,
the response data should has been arrived client-side cache and you can use
"Response" and "ResponseStream" to read them.
Another question you mentioned:
==============
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.
===============
"http://localhost:58549/post_handler.ashx" is not a built-in loopback, it
is a test handler in my test project(I normally build some simple test page
or handler for such testing). And for ASP.NET web page, since it has
viewstate validation for post request(postback), you need to do additional
work if you want to programmtically use WebRequest to send post message to
aspx page. Therefore, I use a "ashx" httphandler here for test since it
doesn't have viewstate validation. The ashx "simple handler" is a
built-in component type you can use in Visual Studio ASP.NET 2.0 project.
If you have interests, you can create one through the following steps in
your VS 2005/ASP.NET project:
** in project solution explorer, right click project/web site node and
chooe "add--new item"
** In the "new item" dialog, choose "Generic Handler" component and click
OK to create one
** It is just an httphandler, but since "ashx" extension has been
registered in IIS already, you do not need to do IIS registering work and
just configure it in web.config file and you can use such handler
If you still have anything unclear, please feel free to let me know. We'are
always willing and pleased to help others resolve their problem and make
the community better and also need and appreciate your understanding and
support to us!
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Date: Fri, 31 Aug 2007 09:07:30 -0400
From: cj <cj@xxxxxxxxxxxxx>
User-Agent: Thunderbird 2.0.0.6 (Windows/20070728)
maybe
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.
Steven Cheng[MSFT] wrote:
Thanks for your reply Cj,
First, I have never added comments against the url you added, I think
aCor Ligthert have some suggestion on this(not Steven Cheng).
Second, about the test code snippet I posted, I've mentioned that it is
Isimple test which post no data(no name&value pairs. And in the article
freepasted 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
method)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
itto 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
rights.(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
our
--------------------
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
mecompany's site info. So don't worry about me posting foo.com, to myexamples.
knowledge it doesn't exist and the rest of the string is also just
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.
Cor Ligthert[MVP] wrote:
Steven,
Well done by changing the URL, these kinds of invitations are giving
italways some strange ideas. As Herfried made me yesterday attend on is
youprobably 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]" <stcheng@xxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:Pu0Fqqr6HHA.360@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi Cj,
As for the "how to change code to post code" question, do you mean
targetwant
to make the httpwebrequest component send a http"POST" message to
propertyserver endpoint?
If this is the case, you can simply set HttpWebRequest.Method
https://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.method.ato
"POST"
#HttpWebRequest.Method Property
POST
spx
and here is a simple test method which use webrequest to send HTTP
andrequest 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
withhave exmaples on add some parameters (if you want to send some data
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
.
- Follow-Ups:
- Prev by Date: Re: how to get list of LAN computers in workgroup
- Next by Date: Re: Trouble Using System.Array.ForEach
- Previous by thread: Re: Mail Merge application - I am Stuck and need help
- Next by thread: Re: how can I change this get code to post code
- Index(es):