0

我正在尝试将一些数据发布到 URI 并读取返回的 html 内容。我找到了这段代码,但需要扩展它以包含我的参数。知道怎么做吗?

WebRequest req = WebRequest.Create("http://www.asp.net"); 
WebResponse res = req.GetResponse(); 
StreamReader sr = new StreamReader(res.GetResponseStream()); 
string html = sr.ReadToEnd();
4

1 回答 1

2

使用流将内容写入 webrequest

string data = "username=<value>&password=<value>"; //replace <value>
    byte[] dataStream = Encoding.UTF8.GetBytes(data);
    private string urlPath = "http://xxx.xxx.xxx/manager/";
    string request = urlPath + "index.php/org/get_org_form";
    WebRequest webRequest = WebRequest.Create(request);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = dataStream.Length;  
    Stream newStream=webRequest.GetRequestStream();
    // Send the data.
    newStream.Write(dataStream,0,dataStream.Length);
    newStream.Close();
    WebResponse webResponse = webRequest.GetResponse(); 

或此链接.NET:发送带有数据的 POST 和读取响应的最简单方法可能对您有更多帮助

于 2013-10-22T11:20:53.730 回答