简体   繁体   English

发布到远程URL,读取响应

[英]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.). 我搜索并发现了许多不同的方式将表单数据发布到远程URL(例如这里的例子等)。 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. 我似乎无法找到的方法是这样做并解析来自远程服务器的响应,在我的情况下基本HTML解析。 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? 这是可能的,使用httpresponse还是其他方法? 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. 请提前通知和thanx。

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: 您可以使用WebClient及其UploadValues()方法:

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 你真正想做的是使用WebRequestHttpWebRequest

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. 这是因为当您的代码是Web请求并且您正在向客户端写回复时。 What you're looking for is to be the client and to send a request and receive a response. 您正在寻找的是成为客户并发送请求并收到回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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