首页 > 编程语言 >C#调用http请求,HttpWebRequest添加http请求头信息

C#调用http请求,HttpWebRequest添加http请求头信息

时间:2023-01-05 11:55:14浏览次数:50  
标签:http 请求 C# request retString HttpWebRequest response string

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace http
{
    class Program
    {
        static void Main(string[] args)
        {
            string Datas = @"{ ""cainfo"": ""1"", ""dev_no"": ""1"", ""dev_safe_info"": ""1"", ""fixmedins_code"": ""060070"", ""fixmedins_name"": ""朝阳医院"", ""inf_time"": ""2020-09-29 02:49:11"", ""infno"": ""1101"", ""infver"": ""V1.0"", ""insuplc_admdvs"": ""340600"", ""mdtrtarea_admvs"": ""340600"", ""msgid"": ""1232342423"", ""opter"": ""14"", ""opter_name"": ""csb-测试"", ""opter_type"": ""1"", ""recer_sys_code"": ""1"", ""sign_no"": ""1"", ""signtype"": ""1"" }";
            //调用post请求
            string retString = httpPost("http://127.0.0.1/admin/ajax/test", Datas);

            //调用get请求
            string retString2 = httpGet("http://127.0.0.1/admin/ajax/test");
            //http://csb-broker.cloud.ahhb.hsip.gov.cn:8086/1.0.0/hsa-fsi-1101

            Console.WriteLine(retString);
            Console.WriteLine("--===========↑上面是post请求返回的数据 ↓下面是get请求返回的数据===============");
            Console.WriteLine(retString2);
            Console.ReadKey();
        }


        public static string httpPost(string url,string data) {

            string retString = String.Empty;
            string paramData = data;
            //CookieContainer cookie = new CookieContainer();
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://csb-broker.cloud.ahhb.hsip.gov.cn:8086/1.0.0/hsa-fsi-1101");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            //request.Referer = Referer;
            byte[] bytes = Encoding.UTF8.GetBytes(paramData);
            request.Accept = "text/html, application/xhtml+xml, */*";
            //request.ContentType = "application/*";
            request.ContentType = "application/x-www-form-urlencoded";
            //设置C# HttpWebRequest post请求头消息
            request.Headers.Add("version", "1.0.0");
            request.Headers.Add("ak", "45b83b3b780d46028afa1da41dcdd0a4");
            request.Headers.Add("sk", "1+JX1Gsg3hEu4Ui+VdQvWlaz1gY=");
            request.ContentLength = bytes.Length;
            try
            {
                Stream myResponseStream = request.GetRequestStream();
                myResponseStream.Write(bytes, 0, bytes.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                retString = myStreamReader.ReadToEnd();
                int statusCode = (int)response.StatusCode;
                if (statusCode == 200)
                {
                    // Console.WriteLine(statusCode);
                    myStreamReader.Close();
                    myResponseStream.Close();

                    if (response != null)
                    {
                        response.Close();
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
            }
            catch (Exception ex)
            {
                //抛出异常返回具体错误消息
                retString =  ex.Message;
            }
            return retString;
        }


        public static string httpGet(string Url)
        {
            string retString = string.Empty;
            //System.GC.Collect();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Proxy = null;
            request.KeepAlive = false;
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }catch(Exception ex){
                //抛出异常返回具体错误消息
                retString = ex.Message;
            }
            return retString;
        }
    }
}

原作者,https://www.cnblogs.com/felix-wang/p/15465073.html  感谢分享

标签:http,请求,C#,request,retString,HttpWebRequest,response,string
From: https://www.cnblogs.com/SDdemon/p/17027139.html

相关文章

  • 海光芯片+麒麟操作系统v10部署docker
    1.docker离线部署参考文档https://codeleading.com/article/9515573273/2.docker离线包下载地址http://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/3.......
  • 【题解】CF700E Cool Slogans
    原题面很简洁,懒得概括了。思路后缀自动机+结论。这种题出题人没有十年脑血栓都没法出。结论1:\(s_{i-1}\)必定是\(s_i\)的后缀。考虑\(s_i\)中\(s_{i-1......
  • 微软次时代新版cmd——windows terminal
    微软次时代新版cmdWindowsTerminal-MicrosoftStore应用程序microsoft/terminal:ThenewWindowsTerminalandtheoriginalWindowsconsolehost,allinthesa......
  • 如何通过Java应用程序将Word转为Excel
    平时在工作中,很多小伙伴会习惯性地将文件保存为Word文档格式,但有时会发现某些文件如果保存成Excel表格可能会更好地呈现。例如有的文本在Word文本中不如在Excel工作表编辑......
  • 以修改注册表的方式避免ACK确认机制带来的延时现象
    TCP本身属面向链接的通讯协议。通讯双方的每一个收发动作,需要以通讯链路正常为前提。因此TCP协议内部提供了默认的ACK验证机制。假定A、B之间存在一条TCP通讯链路,某一时刻A......
  • C#连接数据库实现开发图书管理系统操作代码
     //客户端登录界面(Form1.cs窗口体系) usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem......
  • C# 多线程学习笔记
     ///进程:程序在服务器上运行是,占据的计算资源合集,称之为进程;///进程之间不会相互干扰--进程之间的通信比较困难(分布式)///线程:程序执行的最小单位,相应操作的最小执......
  • docker 安装
    通过YUM进行docker的安装在centos系统中,我们一般通过yum进行软件包的安装。因此本次实验我们也通过yum来安装docker运行环境1、首先通过yum安装必要的依赖组件。为了编......
  • C#获得Access数据库中的表名,以及根据表名获取字段名
    ///<summary>///返回Mdb数据库中所有表表名///</summary>publicstring[]GetShemaTableName(stringdatabase_path,stringdatabase_password......
  • javascript array 数组 indexOf
    javascriptarray数组indexOf低版本的array是没有indexOf的。参考链接:https://reference.codeproject.com/javascript/Reference/Global_Objects/Array/indexOf/......