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