码迷,mamicode.com
首页 > Web开发 > 详细

HttpWebRequest

时间:2014-05-26 15:49:01      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:c   ext   http   a   get   string   

HttpWebRequest request;
HttpWebResponse response;
ASCIIEncoding encoding = new ASCIIEncoding();
request = WebRequest.Create(postUrl) as HttpWebRequest;
byte[] b = encoding.GetBytes(postUrl);
request.UserAgent = "Mozilla/4.0";
request.Method = "PUT";
request.ContentLength = b.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
}
try
{
//获取服务器返回的资源
response = request.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string result = reader.ReadToEnd();
if (result.Trim().Length == 0)
{
textBox2.Text = "执行成功,但未检测到任何返回信息!";
}
else
{
textBox2.Text = result;
}
}
}
catch (WebException ex)
{
textBox2.Text = ex.ToString();
}

 

 

 

 

 

 

 

HttpWebRequest request;
HttpWebResponse response;
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = "Mozilla/4.0";
request.KeepAlive = true;
response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
if (result.Trim().Length == 0)
{
textBox2.Text = "执行成功,但未检测到任何返回信息!";
}
else
{
textBox2.Text = result;
}

HttpWebRequest,布布扣,bubuko.com

HttpWebRequest

标签:c   ext   http   a   get   string   

原文地址:http://www.cnblogs.com/liyangLife/p/3746084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!