C#的远程post与get

 1             //接收
 2             Stream stream = HttpContext.Current.Request.InputStream;
 3             byte[] getByte = new byte[stream.Length];
 4             stream.Read(getByte, 0, getByte.Length);
 5             string postStr = Encoding.UTF8.GetString(getByte);
 6             //请求
 7             string url = "";
 8             HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
 9             webrequest.Method = "post";
10             webrequest.ContentType = "application/json;charset=UTF-8";
11             byte[] postByte = Encoding.UTF8.GetBytes(postStr);
12             webrequest.ContentLength = postByte.Length;
13             Stream stream1 = webrequest.GetRequestStream();
14             stream1.Write(postByte, 0, postByte.Length);
15             stream1.Close();
16             //响应
17             HttpWebResponse response;
18             try
19             {
20                 response = (HttpWebResponse)webrequest.GetResponse();
21             }
22             catch (WebException ex)
23             {
24                 response = (HttpWebResponse)ex.Response;//返回远程服务器报告回来的错误
25             }
26             StreamReader sr = new StreamReader(response.GetResponseStream());
27             string getStr = sr.ReadToEnd();

 

posted @ 2017-10-26 14:19  圆圆娃哈哈  阅读(418)  评论(0编辑  收藏  举报
Live2D