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

组织http请求

时间:2015-03-03 20:11:36      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

post方式

string stratTime="";
string end=""://要拼接的参数
string    postURL = "http://www.baidu.com?startTime=" + start + "&endTime=" + end;
Encoding dataEncode=Encoding.UTF8;//声明字符格式
byte[] by=dataEncode.GetBytes(postURL);//url转换
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postURL));
webReq.Method = "post";
webReq.ContentType = "application/json";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string ret = string.Empty;//接受返回的json数据
ret = sr.ReadToEnd();
newStream.Close();
response.Close();

get

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postURL);
request.ContentType = "application/json";
request.Method = "get";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
instream.Close(); 

 

组织http请求

标签:

原文地址:http://www.cnblogs.com/valiant1882331/p/4311707.html

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