当前位置:   article > 正文

利用HttpWebRequest发送HTTP请求

利用HttpWebRequest发送HTTP请求
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                string param = BuildParamGetPatientInfoForSDEY(inHospitalID, caseNum); 
                logInfo.LogStepDetail(cCfg, "请求的JSON内容:", param);

                Encoding encoding = Encoding.UTF8;
                byte[] byteArray = Encoding.UTF8.GetBytes(param);
                req.Method = "POST";
                req.Headers.Add("access_token", token);
                req.ContentType = "application/json";
                req.ContentLength = byteArray.Length;
                req.Accept = "*/*";
                req.Timeout = 30 * 1000;
                req.ReadWriteTimeout = 30 * 1000;
                req.ServicePoint.Expect100Continue = false;

                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(byteArray, 0, byteArray.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        responseDate = reader.ReadToEnd();
                        logInfo.LogStepDetail(cCfg, "POST请求的返回结果", responseDate);
                    }
                }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/748387
推荐阅读
相关标签
  

闽ICP备14008679号