HTTP POST ASP .NET

T

Timothy Bales

Could someone PLEASE explain how do I use HTTP POST in ASP .NET to get an
xml string from a server that exposes a web service? My IIS server will
receive a username and password from an HTML form and will have to send that
info via HTTP POST to another server that exposes the web service, I then
will have to parse the XML output received from the remote web service,
format it and send to the user on a web page. My IIS server is in the
middle, I need to pass the user info to the remote web service and then the
XML output back from the remote web service to the end user's browser.



Just a snippet of code for the background HTTP POST in ASP .NET, I have
checked a lot of ASP .NET books and none of them touches on this scenario.



THANKS,



Tim.
 
K

Ken Cox [Microsoft MVP]

Hi Tim,

I like using WebClient for that. You need to include the name of the
parameter that the target page expects.

Here's some sample code that might get you started. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

(See also
http://msdn.microsoft.com/library/d...ef/html/frlrfsystemnetwebclientclasstopic.asp )

Sub DoPost()
Dim uriString As String = _
"http://localhost/p4320work/mySpecialPage.aspx"
Dim strGoName As String
strGoName = TextBox1.Text
' Create a new WebClient instance.
Dim myWebClient As New System.Net.WebClient
Dim myNameValueCollection As New _
System.Collections.Specialized.NameValueCollection
myNameValueCollection.Add("go1", strGoName)
myNameValueCollection.Add("Button1", "")
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)
' Response.Redirect("http://msdn.microsoft.com/")
End Sub


Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Call DoPost()
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server">test</asp:TextBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
</form>
 
K

Ken Cox [Microsoft MVP]

Hmmm... you might want this version instead if you need to see the
response...

Sub DoPost()
Dim uriString As String = _
"http://p4320/p4320work/login.aspx"
' Create a new WebClient instance.
Dim myWebClient As New System.Net.WebClient
Dim myNameValueCollection As New _
System.Collections.Specialized.NameValueCollection
myNameValueCollection.Add("txtName", "Ken")
myNameValueCollection.Add("txtPwd", "password")
myNameValueCollection.Add("Button1", "")
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)
Label1.Text = "Response received was :" & _
System.Text.Encoding.ASCII.GetString(responseArray)
End Sub
 
T

Timothy Bales

Guys I really appreciate your help. I'm using WebClient and have finally
been able to do what I wanted to do. What has me puzzled right now is that I
was not expecting formatted XML output but at least the description of the
fields and I'm only getting their values. When I convert from Byte to String
I get a long string with all the values concatenated. Is this right? When I
query the server from the browser address bar using the same parameters that
I send programmatically I get a complete XML document with the field names
and everything not only the values.

Again, thanks a lot.

Tim.
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top