• C# 主动发起请求代码


    本文用于写代码是给提示

     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 }
  • 相关阅读:
    文件拒绝访问且无法显示当前所有者
    SpringBoot+MyBatis整合中的坑以及Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required错误详解
    Android 免费批量导入Excel姓名手机号码为通讯录联系人
    批量压缩打包文件夹到各自的压缩包中
    GNU make 官方手册
    wps 删除线的快捷键
    【K8S】Kubernetes的基本数据存储详解EmptyDir / HostPath / NFS
    【K8S】kubernetes 一些概念的介绍
    【K8S】Kubernetes系统初始化与部署
    印尼医疗龙头企业Halodoc的数据平台转型之路:数据平台V1.0
  • 原文地址:https://www.cnblogs.com/waterBlog/p/3678742.html
Copyright © 2020-2023  润新知