本文用于写代码是给提示
1 private string GetHW(string uri) 2 { 3 string returnStr = ""; 4 HttpWebRequest hwRequest = (HttpWebRequest)System.Net.WebRequest.Create(uri); 5 hwRequest.Method = "GET"; 6 HttpWebResponse hwRespone = (HttpWebResponse)hwRequest.GetResponse(); 7 if (hwRespone != null && hwRespone.StatusCode == HttpStatusCode.OK) 8 { 9 //流的处理有必要还有进行解压缩之类的 10 using (StreamReader sr = new StreamReader(hwRespone.GetResponseStream(), Encoding.UTF8)) 11 { 12 returnStr = sr.ReadToEnd(); 13 } 14 } 15 return returnStr; 16 } 17 18 private string PostHW(string uri, string postString) 19 { 20 string returnStr = ""; 21 HttpWebRequest hwRequest = (HttpWebRequest)System.Net.WebRequest.Create(uri); 22 hwRequest.Method = "Post"; 23 hwRequest.ContentType = "application/x-www-form-urlencoded"; //设置ContntType ,这句很重要,否则无法传递参数 24 25 byte[] postData = Encoding.UTF8.GetBytes(postString);//postData即为发送的数据 26 hwRequest.ContentLength = postData.Length;//设置客户端发送的内容长度 27 28 Stream requestStream = hwRequest.GetRequestStream();//获取请求流 29 requestStream.Write(postData, 0, postData.Length);//把参数写入流 30 requestStream.Close();//关闭请求流 31 32 HttpWebResponse hwRespone = (HttpWebResponse)hwRequest.GetResponse(); 33 Stream responseStream = hwRespone.GetResponseStream();//得到回应流 34 35 if (hwRespone != null && hwRespone.StatusCode == HttpStatusCode.OK) 36 { 37 using (StreamReader sr = new StreamReader(hwRespone.GetResponseStream(), Encoding.UTF8)) 38 { 39 returnStr = sr.ReadToEnd(); 40 } 41 } 42 responseStream.Close();//关闭回应流 43 return returnStr; 44 } 45 46 private string GetWebClient(string uri) 47 { 48 string returnStr = ""; 49 WebClient wCient = new System.Net.WebClient(); 50 wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 51 using (StreamReader sr = new StreamReader(wCient.OpenRead(uri), Encoding.UTF8)) 52 { 53 returnStr = sr.ReadToEnd(); 54 } 55 return returnStr; 56 } 57 public string GetWebClient2(string uri) 58 { 59 uri = @""; 60 string returnStr = ""; 61 WebClient wCient = new System.Net.WebClient(); 62 wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 63 byte[] responseData = wCient.DownloadData(uri); 64 //using (StreamReader sr = new StreamReader(wCient.OpenRead(uri), Encoding.UTF8)) 65 //{ 66 // returnStr = sr.ReadToEnd(); 67 //} 68 returnStr = Encoding.UTF8.GetString(responseData);//返回接受的数据 69 return returnStr; 70 } 71 72 private string PostWebClient(string uri, string postString) 73 { 74 string returnStr = ""; 75 //string postString = "userName=123&city=101";//设置参数,POST参数的格式是 参数1=值1&参数2=值2&参数3=值3 //PostString = "blog_title=博客标题&";PostString += "blog_body=博客内容&";PostString += "blog_class=1";//将参数字符串转换成字节数组。 76 77 WebClient wCient = new System.Net.WebClient(); 78 wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 79 byte[] postData = Encoding.ASCII.GetBytes(postString); 80 byte[] responseData = wCient.UploadData(uri, "POST", postData);//得到返回字符流 81 returnStr = Encoding.UTF8.GetString(responseData);//返回接受的数据 82 return returnStr; 83 }