2013-09-27 154 views
0

與WebRequest對象提交的數據,我有以下代碼:在get方法

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

我如何提交與數據GET請求?

回答

1

當使用GET方法時,您應該將數據放入網址中。

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