如何使用Web请求GET方法asp.net在url中传递值

时间:2012-11-05 10:58:45

标签: asp.net web-services

我是.Net和web服务的新手..我喜欢通过url..how来传递id吗?无论是发帖还是获取方法?指导我

string url = "http://XXXXX//"+id=22;

WebRequest request = WebRequest.Create(url);
request.Proxy.Credentials = new NetworkCredential(xxxxx);
request.Credentials = CredentialCache.DefaultCredentials;
//add properties
request.Method = "GET";
request.ContentType = "application/json";
//convert 
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
//post data
Stream streamdata = request.GetRequestStream();
streamdata.Write(byteArray, 0, byteArray.Length);
streamdata.Close();
//response
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams and the response.
reader.Close();
response.Close();

1 个答案:

答案 0 :(得分:0)

如果您只想构建带参数的URL(参考:查询字符串),那么您只需执行以下操作:

string url = string.Format("http://www.google.com/?id={0}",22);

You can check this url, Passing variables between pages using QueryString at Code Project