zoukankan      html  css  js  c++  java
  • C#请求API数据

    1. 后台发出Http 请求
      1. Get无参请求

    //1.创建url

       var url = "网址";

    //2.天加链接

       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    //3.创建一个响应,存储请求获取的响应, 响应=请求获取的响应

       HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    //4.创建一个变量,接收响应的数据

       var responseText = response.GetResponseStream();

    //5.使用流读取信息,并赋值给一个变量

       var text = new StreamReader(responseText).ReadToEnd();

    1. Get有参请求

    var url = "网址

    ?page=1&rows=10&sort=indexs&SearchName=";

    var postDate = "page=1&rows=10&sort=indexs&SearchName=";

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

    request.Method = "get";

    HttpWebResponse response = (HttpWebResponse) request.GetResponse();

    var text = response.GetResponseStream();

    var sr=new StreamReader(text).ReadToEnd();

    1. Post有参提交

       1.创建请求连接

    Var url="http://www.leadnt.com/recepticle.aspx";

          var request = (HttpWebRequest)WebRequest.Create();

       2.添加参数

            var postData = "thing1=hello";

            postData += "&thing2=world";

      3.将参数转换编码格式

            var data = Encoding.ASCII.GetBytes(postData);

      4.设置属性

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            request.ContentLength = data.Length;

      5.将转换后的参数以文件流的形式写入到请求中

            using (var stream = request.GetRequestStream())

            {

              stream.Write(data, 0, data.Length);

            }

      6.声明变量,获取响应

            var response = (HttpWebResponse)request.GetResponse();

      7.以文件流的方式读取响应结果

       var responseString =

     new StreamReader(response.GetResponseStream()).ReadToEnd();

  • 相关阅读:
    第12讲:数据库完整性
    第11讲:视图及其应用
    第10讲:利用SQL语言实现关系代数操作
    ArcEngine 坐标系转换
    [转]ArcGIS计算图斑的四邻坐标(XMin,XMax,YMin,YMax)
    oracle11g 修改字符集 修改为ZHS16GBK
    Oracle 全文索引相关命令
    SQL语句 递归
    流量操控之SSH隧道与端口转发
    VIM 常用操作
  • 原文地址:https://www.cnblogs.com/JueXiaoQiang/p/7809750.html
Copyright © 2011-2022 走看看