0

我有以下代码:

var Instance = WebRequest.Create(new Uri("http://mywebsite.com/page.aspx"));
var Data = new Dictionary<string, string>();
Data["Foo"] = "Bar";
Data["Baz"] = "Paz";

如何提交带有数据的 GET 请求?

4

1 回答 1

1

使用GET方法时,您应该将数据放入 url。

var Data = new Dictionary<string, string>();
Data["Foo"] = "Bar";
Data["Baz"] = "Paz";
UriBuilder uri = new UriBuilder("http://mywebsite.com/page.aspx");
uri.Query = String.Join("&",Data.Select(x=>String.Format("{0}={1}", 
                                          x.Key, HttpUtility.UrlEncode(x.Value))));


var Instance = WebRequest.Create(uri.ToString());

您的网址将是:

http://mywebsite.com:80/page.aspx?Foo=Bar&Baz=Paz
于 2013-09-27T22:29:39.330 回答