winform调用api接口问题

qq_34818047 2016-05-03 10:25:10
winform调用api接口给app传递数据,接口那边写好了,以前都是用的webserver 没写过 api的。提交方式是get,求指点下
...全文
566 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
songbing774933 2016-05-04
  • 打赏
  • 举报
回复
提交数据用get,然后在QueryString里面传jsonstring,这接口我也是醉了....


private static string Get(string strRequestUri ,/* string strReferer = "" ,*/ string strToken = "" , int timeout = 10*1000)
{
// 初始化HttpWebRequest
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strRequestUri);

// 封装Cookie
if ("" != strToken)
{
Uri uri = new Uri(strRequestUri);
Cookie cookie = new Cookie("Token" , strToken); // 设置key、value形式的Cookie
CookieContainer cookies = new CookieContainer( );
cookies.Add(uri , cookie);
httpRequest.CookieContainer = cookies;
}

// 封装Http Header
httpRequest.Method = "Get";
//httpRequest.Referer = strReferer;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36";
httpRequest.Accept = "text/plain, */*; q=0.01";
httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpRequest.Timeout = timeout;
httpRequest.KeepAlive = true;
httpRequest.ContentLength = 0;

// 获得应答报文
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse( );
Stream responseStream = httpResponse.GetResponseStream( );
StreamReader reader = new StreamReader(responseStream , System.Text.Encoding.UTF8);
string strResponse = reader.ReadToEnd( );
reader.Close( );
responseStream.Close( );

return strResponse;
}


public static string Post(string strRequestUri , string strContent ,/* string strReferer = "" ,*/ string strToken = "" , int timeout = 10*1000)
{
// 初始化HttpWebRequest
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strRequestUri);

// 封装Cookie
if ("" != strToken)
{
Uri uri = new Uri(strRequestUri);
Cookie cookie = new Cookie("Token" , strToken); // 设置key、value形式的Cookie
CookieContainer cookies = new CookieContainer( );
cookies.Add(uri , cookie);
httpRequest.CookieContainer = cookies;
}

// 封装Http Header
httpRequest.Method = "Post";
//httpRequest.Referer = strReferer;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36";
httpRequest.Accept = "text/plain, */*; q=0.01";
httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpRequest.Timeout = timeout;
httpRequest.KeepAlive = true;

// 通过流写入请求数据
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strContent); // 编码形式按照个人需求来设置
httpRequest.ContentLength = bytes.Length;
Stream requestStream = httpRequest.GetRequestStream( );
requestStream.Write(bytes , 0 , bytes.Length);
requestStream.Close( ); // 不要忘记关闭流

// 获得应答报文
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse( );
Stream responseStream = httpResponse.GetResponseStream( );
StreamReader reader = new StreamReader(responseStream , System.Text.Encoding.UTF8);
string strResponse = reader.ReadToEnd( );
reader.Close( );
responseStream.Close( );


return strResponse;
}
lvfeng_95 2016-05-04
  • 打赏
  • 举报
回复
request调用response接收
下雨天抽烟 2016-05-04
  • 打赏
  • 举报
回复
首先你要理解,winform调用的接口是服务器借口,不是app 的接口。这个接口肯定是完成订单后,处理数据库数据,然后app 通过刷新数据去获取数据库中已处理的订单信息。 关于这个get/post 的.net实现代码网上有好多的。网页一般用ajax 实现或者 在后台拼接一个form,提交一下就可以了。winform可以采用form 拼接的方式。
为轮子而生 2016-05-04
  • 打赏
  • 举报
回复
WebClient.UploadData方法
qq_34818047 2016-05-03
  • 打赏
  • 举报
回复
引用 1 楼 LeiRobin 的回复:
为啥要用winfrom调用API然后给APP数据?为毛不直接让APP调呢?
我们这边原有的分销系统 现在要分销商每销售一单开单完成后调用api给app那边传递数据。具体怎么考虑的是经理那边决定的,我只接到这个任务要实现。
姓小名白丶 2016-05-03
  • 打赏
  • 举报
回复
为啥要用winfrom调用API然后给APP数据?为毛不直接让APP调呢?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧