HTTP POST请求后面的代码

时间:2013-10-22 15:56:36

标签: .net http webforms http-post

有一个外部Web服务,我的页面是.NET webforms 我需要通过POST从我的页面传递参数来调用这个web服务 其中一些参数我需要填写代码。包括input type="file"

我的问题是:
如何使用POST方法将请求发布到webservice?
是否可以通过代码隐藏,或者我必须通过JavaScript提交?

1 个答案:

答案 0 :(得分:0)

基于马文和大卫回答 - 评论:

可以使用WebClientHttpWebRequest。 据我所知,WebClient继承了HttpWebRequest,提供了更简单的代码方式。

一些例子:

Using client As New Net.WebClient
    Dim reqparm As New Specialized.NameValueCollection
    reqparm.Add("param1", "somevalue")
    reqparm.Add("param2", "othervalue")
    Dim responsebytes = client.UploadValues(someurl, "POST", reqparm)
    Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
End Using

参考:Sending HTTP POST with System.Net.WebClient

string postData = Console.ReadLine();
using (System.Net.WebClient wc = new System.Net.WebClient())
{
    wc.Headers.Add("Content-Type","application/x-www-form-urlencoded");
    // Upload the input string using the HTTP 1.0 POST method.
    byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData);
    byte[] byteResult= wc.UploadData("http://targetwebiste","POST",byteArray);
    // Decode and display the result.
    Console.WriteLine("\nResult received was {0}",
                      Encoding.ASCII.GetString(byteResult));
}

参考:Send POST with WebClient.DownloadString in C#

StackOverflow中的其他参考:
.NET: WebBrowser, WebClient, WebRequest, HTTPWebRequest... ARGH!
WebClient vs. HttpWebRequest/HttpWebResponse