C# HttpWebReqeust和HttpWebResponse发送请求

 1   var request = (HttpWebRequest)WebRequest.Create("URL");
 2 
 3             var data = Encoding.UTF8.GetBytes("username=zhenjinggu&md5Pwd=643d1820b9ce49bbf283b4875848596c&otpCode=");
 4 
 5             request.Method = WebRequestMethods.Http.Post;
 6             request.Timeout = 60 * 1000;
 7             request.AllowAutoRedirect = true;
 8             request.ContentLength = data.Length;
 9             request.ContentType = "application/x-www-form-urlencoded";
10             request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
11             request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36";
12             var stream = request.GetRequestStream();
13             stream.Write(data, 0, data.Length);
14             stream.Close();
15 
16             var response = (HttpWebResponse)request.GetResponse();
17             var sr = new StreamReader(response.GetResponseStream());
18             var content = sr.ReadToEnd();

 

posted @ 2016-03-16 15:37  the boy、图样图森破  阅读(651)  评论(0编辑  收藏  举报