简体   繁体   中英

Post to remote URL, read response

I have searched and found many different ways to post form data to a remote URL (example here , etc.). What I cant seem to find is a way to do this AND parse the response from the remote server, in my case basic HTML to parse. Just to be clear I dont need help with parsing the data, I dont know how to get the response in a way I can then use. Is this possible, using httpresponse, or some other method? I seem to recall doing similar years ago, but only in a past life, and then, only in legend. Please advise and thanx in advance.

Sample response from server:

<H1>FormResponse</H1><BR>
<CENTER><B><I>Transaction was successful!</I></B></CENTER><BR>
<B>Transaction ID: </B>TP1H5X06T9RT5T<BR><B>Post Status: </B>Queued<BR>

You can use WebClient and its UploadValues() method:

using (WebClient client = new WebClient())
{
     NameValueCollection nvc = new NameValueCollection()
     {
         { "foo", "bar"}
     };

     byte[] responseBytes = client.UploadValues("http://www.google.com", nvc);
     string response = System.Text.Encoding.ASCII.GetString(responseBytes);
}

What you really want to do is use WebRequest or HttpWebRequest

I suggest you start here

To clarify, the link you posted is not what you're looking for. That's for when your code IS the web request and you're writing a response to the client. What you're looking for is to be the client and to send a request and receive a response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM