关于C# winform怎么调用webapi来获取到json数据
C/S系统也可以和B/S系统一样实现“前后端分离”,那这样写winform就相当于纯粹的前端页面了,然后再单独部署一个webapi项目,通过api调用数据库进行数据的操作,有利于维护和数据安全性的提高,那么winform怎么去调用api接口呢,写了一个demo,大家借鉴一下哈,本人才疏学浅,有不足和错误请指出:
winform界面就不设计了,仅仅是为了测试是否调用到api,直接在创建的类库中写一个方法:
- /// <summary>
- /// 调用api返回json
- /// </summary>
- /// <param name="url">api地址</param>
- /// <param name="jsonstr">接收参数</param>
- /// <param name="type">类型</param>
- /// <returns></returns>
- public static string HttpApi(string url, string jsonstr, string type)
- {
- Encoding encoding = Encoding.UTF8;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
- request.Accept = "text/html,application/xhtml+xml,*/*";
- request.ContentType = "application/json";
- request.Method = type.ToUpper().ToString();//get或者post
- byte[] buffer = encoding.GetBytes(jsonstr);
- request.ContentLength = buffer.Length;
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
- {
- return reader.ReadToEnd();
- }
- }
然后再winform界面的事件中调用该方法:
- private void button3_Click(object sender, EventArgs e)
- {
- string url = "...(此处为api端口)/api/VisitorInfo/GetEndVisitorInfoList";
- var str = ApiHelper.HttpApi(url, "{}", "get");
- }
本地运行后变量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();
特此记录!
出处:https://blog.csdn.net/Andrewniu/article/details/79752653
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/10437993.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2019-02-26 16:04 jack_Meng 阅读(19658) 评论(1) 编辑 收藏 举报