关于C# winform怎么调用webapi来获取到json数据




C/S系统也可以和B/S系统一样实现“前后端分离”,那这样写winform就相当于纯粹的前端页面了,然后再单独部署一个webapi项目,通过api调用数据库进行数据的操作,有利于维护和数据安全性的提高,那么winform怎么去调用api接口呢,写了一个demo,大家借鉴一下哈,本人才疏学浅,有不足和错误请指出:

        winform界面就不设计了,仅仅是为了测试是否调用到api,直接在创建的类库中写一个方法:

[csharp] view plain copy
  1.        /// <summary>  
  2.        /// 调用api返回json  
  3.        /// </summary>  
  4.        /// <param name="url">api地址</param>  
  5.        /// <param name="jsonstr">接收参数</param>  
  6.        /// <param name="type">类型</param>  
  7.        /// <returns></returns>  
  8.        public static string HttpApi(string url, string jsonstr, string type)  
  9.        {  
  10.            Encoding encoding = Encoding.UTF8;  
  11.            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
  12.            request.Accept = "text/html,application/xhtml+xml,*/*";  
  13.            request.ContentType = "application/json";  
  14.            request.Method = type.ToUpper().ToString();//get或者post  
  15.            byte[] buffer = encoding.GetBytes(jsonstr);  
  16.            request.ContentLength = buffer.Length;  
  17.            request.GetRequestStream().Write(buffer, 0, buffer.Length);  
  18.            HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  19.            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))  
  20.            {  
  21.                return reader.ReadToEnd();  
  22.            }  
  23.        }  

然后再winform界面的事件中调用该方法:

[csharp] view plain copy
  1. private void button3_Click(object sender, EventArgs e)  
  2.         {  
  3.             string url = "...(此处为api端口)/api/VisitorInfo/GetEndVisitorInfoList";  
  4.             var str = ApiHelper.HttpApi(url, "{}""get");  
  5.         }  

本地运行后变量str接收数据格式如下:


HttpWebRequest请求时无法发送具有此谓词类型的内容正文。

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
28
29
30
31
32
33
34
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl); //--需要封装的参数
             request.CookieContainer = new CookieContainer();
             CookieContainer cookie = request.CookieContainer; //如果用不到Cookie,删去即可 
             //以下是发送的http头
             request.Referer = "" ;
             request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ;
             request.Headers[ "Accept-Language" ] = "zh-CN,zh;q=0." ;
             request.Headers[ "Accept-Charset" ] = "GBK,utf-8;q=0.7,*;q=0.3" ;
             request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1" ;
             request.KeepAlive = true ;
             //上面的http头看情况而定,但是下面俩必须加 
             request.ContentType = "application/x-www-form-urlencoded" ;
             Encoding encoding = Encoding.UTF8; //根据网站的编码自定义
             request.Method = "get" //--需要将get改为post才可行
             string postDataStr;
             Stream requestStream = request.GetRequestStream();
             if (postDatas != "" )
             {
                 postDataStr=postDatas; //--需要封装,形式如“arg=arg1&arg2=arg2”
                 byte [] postData = encoding.GetBytes(postDataStr); //postDataStr即为发送的数据,
                 request.ContentLength = postData.Length;
                 requestStream.Write(postData, 0, postData.Length);
             }
 
             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
             Stream responseStream = response.GetResponseStream();
 
 
             StreamReader streamReader = new StreamReader(responseStream, encoding);
             string retString = streamReader.ReadToEnd();
 
             streamReader.Close();
             responseStream.Close();
             return retString;

  如果是以流的方式提交表单数据的时候不能使用get方法,必须用post方法,

 

改为

1
request.Method = "post" ;  就可以了。  做一个记号


今天请求接口直接调了以前写好的方法,结果报了(405)不支持方法的错误,一看是GET写成POST了,改成GET之后,又报了无法发送具有此谓词类型的内容正文错误的错误

原来之前的方法里面有GetRequestStream(), GET请求并不支持它。

把GetRequestStream()和相应的代码注释掉就OK了

      // Stream outStream = myRequest.GetRequestStream();
      // outStream.Write(arrB, 0, arrB.Length);
      //outStream.Close();

特此记录!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值