C# 使用HTTP发送POST、GET请求

实习单位需要测试c#与单位某软件的对接情况,让我负责c#通过HTTP发送POST、GET请求

C# POST、GET请求主要涉及两个类HttpWebRequest和HttpWebResponse,前者用来写入数据,后者用来接收返回数据

代码如下:

POST:
 public string putMessage(string message)
        {
            try
            {
                string Url = "";//自己指定URL
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(message);
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(message);
                myStreamWriter.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response.Server + "  " + response.Method + "  " + response.StatusDescription;
            }
            catch (WebException ex)
            {
                var error = (HttpWebResponse)ex.Response;
                Stream myResponseStream = error.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
        }

由于需求不需要接收POST返回,所以没有读取POST返回的流,如果要读取POST返回的流,参考代码如下

 public string putMessage(string message)
        {
            try
            {
                string Url = "http://" + IP_PORT + "/cmsp/message?token=" + TOKEN + "&topic=" + TOPIC_NAME;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";// 指定提交数据类型
                request.ContentLength = Encoding.UTF8.GetByteCount(message);
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(message);
                myStreamWriter.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            catch (WebException ex)
            {
                var error = (HttpWebResponse)ex.Response;
                Stream myResponseStream = error.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
        }
GET:
 public string getMessage()
        {
            try
            {
                string Url = "";//自己指定URL
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "GET";
                request.ContentType = "text/json;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            catch (WebException ex)
            {
                var error = (HttpWebResponse)ex.Response;
                Stream myResponseStream = error.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }           
        }

这种catch的写法可以获取报错之后服务器的返回数据,如果服务器返回错误码和错误原因的话这样是能获取到的。

参考资料

  1. HttpRequest中常见的四种ContentType【转载】
  2. C# 之 HttpWebResponse类
  3. C#发送和接收HTTP请求

你可能感兴趣的:(C#笔记)