java 调用c# web api 代码
上次我们写的.net web api 给对方公司的java团队调用,他们觉得说java无法调用.net 写的api ,靠居然有这事,索性自己写一个java的demo给他们
使用apache的HttpClient插件,下载导入对应jar包
参考:
http://hc.apache.org/httpcomponents-client-ga/quickstart.html
//package org.apache.http.examples.client; import java.io.File;
import java.io.FileInputStream; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.*; public class MyClientDemo {
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault();
try { HttpPost httpPost = new HttpPost("http://IP/Topevery.CAD.Web.Api/api/user/GetGroupInfo");
String json = "{\r\n" +
"\"account\":\"abc\",\r\n" +
"\"password\":\"abc\",\r\n" +
"\"grade\":1\r\n" +
"}";
StringEntity requestEntity = new StringEntity(json,"utf-8");
requestEntity.setContentEncoding("UTF-8");
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(requestEntity); System.out.println("Executing request: " + httpPost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println("result:" + EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}
}
finally {
httpclient.close();
}
}
}
调用结果如下

http请求推荐使用 http://square.github.io/okhttp/
java 调用c# web api 代码的更多相关文章
- Java调用WeChat's API总结
微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务:而后者仅仅能够浏览页面,通过用户的输入信息与用户互动. 本人根 ...
- java调用百度地图API依据地理位置中文获取经纬度
百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...
- java调用高德地图api实现通过ip定位访问者的城市
所需东西:高德地图的key 注意:这个key是 web服务的key 和js的key不是一个key(若没有则自行创建,创建教程在文末) 高德地图的api文档:https://lbs.amap.com/ ...
- Geocoding java调用百度地图API v2.0 图文 实例( 解决102错误)
如何使用? 第一步:申请ak(即获取密钥),若无百度账号则首先需要注册百度账号. 第二步,拼写发送http请求的url,注意需使用第一步申请的ak. 第三步,接收http请求返回的数据(支持json和 ...
- HttpResponseMessage 调用.net web api
// // GET: /Home/ //释迦苦僧 public ActionResult Index() { HttpClient client = new HttpClient(); client. ...
- java调用支付宝 支付api 【沙箱环境】
由于支付宝支付api需要各种备案,但学校项目需要引入支付功能 先演示 ,所以采用 沙箱环境 一.登录支付宝 开放平台 及配置相关 https://openhome.alipay.com/platf ...
- java调用百度地图API
http://blog.csdn.net/iTommy2016/article/details/75144936 http://blog.csdn.net/kingken212/article/det ...
- java 调用短信 api 接口发送短信
参考: https://blog.csdn.net/u014793522/article/details/59062014 参考 :https://blog.csdn.net/Lu_shilusi ...
- ASP.NET Web API与Owin OAuth:调用与用户相关的Web API
在前一篇博文中,我们通过以 OAuth 的 Client Credential Grant 授权方式(只验证调用客户端,不验证登录用户)拿到的 Access Token ,成功调用了与用户无关的 We ...
随机推荐
- Ubuntu如何百度云盘下载
我使用Firefox浏览器下载. (1)先为浏览器下载一个插件:网盘助手 (2)通过终端安装aria2: sudo apt-get install python-apt sudo apt-get in ...
- ccf-路径解析201604-3
C++没有split函数 但是有一个简单的方法,利用stringstream构建; 然后这道题就很简单啦 还要注意不能用cin 因为有空行的存在 #include <bits/stdc++.h& ...
- 什么是C/S模式与B/S模式,两者区别与优缺点
转自https://wenwen.sogou.com/z/q1709598292.htm C/S (Client/Server,客户机/服务器)模式又称C/S结构,是软件系统体系结构的一种.C/S模式 ...
- 锚点的animate使用过程中定位不准确的问题小记
源码: $('html, body, .S').animate({ scrollTop: $('.a1').offset().top - 133}, { duration: 1500, easing: ...
- js···DOM2动态创建节点
1.生成节点的方法 document.createElement(“div”) 2.插入节点的方法 父元素.appendChild(新节点) 在父节点中的子节点后面插入新的节点 3.在指定的位置 ...
- Python中serial的使用
一.概述 pyserial模块封装了对串口的访问. 二.特性 在支持的平台上有统一的接口. 通过python属性访问串口设置. 支持不同的字节大小.停止位.校验位和流控 ...
- Appium环境搭建——安装以及运行appium server失败点总结
一.运行Appium失败:未安装.Net Framework 4.5 之前安装AppScan安全测试工具时,就要求.Net 4.5以上环境,我其中一台电脑的系统是Win7-32bit的,安装.Net ...
- web爬虫,BeautifulSoup
BeautifulSoup 该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单. 1 2 3 ...
- ConcurrentDictionary对象
ConcurrentDictionary<int, List<a>> dic = new ConcurrentDictionary<int, List<a>& ...
- GIT & VersionControl
一.Git Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理. [1] Git 是 Linus Torvalds 为了帮助管理 Linux ...