上次我们写的.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 代码的更多相关文章

  1. Java调用WeChat's API总结

    微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务:而后者仅仅能够浏览页面,通过用户的输入信息与用户互动. 本人根 ...

  2. java调用百度地图API依据地理位置中文获取经纬度

    百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...

  3. java调用高德地图api实现通过ip定位访问者的城市

    所需东西:高德地图的key 注意:这个key是 web服务的key  和js的key不是一个key(若没有则自行创建,创建教程在文末) 高德地图的api文档:https://lbs.amap.com/ ...

  4. Geocoding java调用百度地图API v2.0 图文 实例( 解决102错误)

    如何使用? 第一步:申请ak(即获取密钥),若无百度账号则首先需要注册百度账号. 第二步,拼写发送http请求的url,注意需使用第一步申请的ak. 第三步,接收http请求返回的数据(支持json和 ...

  5. HttpResponseMessage 调用.net web api

    // // GET: /Home/ //释迦苦僧 public ActionResult Index() { HttpClient client = new HttpClient(); client. ...

  6. java调用支付宝 支付api 【沙箱环境】

    由于支付宝支付api需要各种备案,但学校项目需要引入支付功能  先演示  ,所以采用 沙箱环境 一.登录支付宝 开放平台 及配置相关 https://openhome.alipay.com/platf ...

  7. java调用百度地图API

    http://blog.csdn.net/iTommy2016/article/details/75144936 http://blog.csdn.net/kingken212/article/det ...

  8. java 调用短信 api 接口发送短信

    参考:   https://blog.csdn.net/u014793522/article/details/59062014 参考 :https://blog.csdn.net/Lu_shilusi ...

  9. ASP.NET Web API与Owin OAuth:调用与用户相关的Web API

    在前一篇博文中,我们通过以 OAuth 的 Client Credential Grant 授权方式(只验证调用客户端,不验证登录用户)拿到的 Access Token ,成功调用了与用户无关的 We ...

随机推荐

  1. python初识模块

    sys import sys   print(sys.argv)     #输出 $ python test.py helo world ['test.py', 'helo', 'world']  # ...

  2. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  3. linux下centos解压时报错: gzip: stdin: not in gzip format   tar: Child returned status 1   tar: Error is not recoverable: exiting now

    最近在linux下安装python时,解压Python.tgz文件时遇到一个问题:          gzip: stdin: not in gzip format      tar: Child r ...

  4. 西门子SCL读写DB数据

    数据块间接寻址方式,仅供参考. STATUS_1:= DB11.DW[COUNTER]; //字节间接寻址STATUS_2:= DB12.DX[WNO, BITNO]; //位间接寻址,用户改变WNO ...

  5. C#中扩展方法的使用

    MSDN中这样定义扩展方法:扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. ...

  6. android 判断横竖屏的方法(转)

    public boolean isScreenChange() { Configuration mConfiguration = this.getResources().getConfiguratio ...

  7. nodejs故障cnpm没反应

    莫名发生的故障cnpm没反应 重新整理nodejs使用流程 方案1 1.安装64位nodejs 2.设置代理 npm config set proxy http://127.0.0.1:9999    ...

  8. 列表中使用嵌套for循环[i*j for i in range(3) for j in range(3)]

    利用嵌套for循环形成一个新列表 [i*j for i in range(3) for j in range(3)]相当于如下代码 li=[] for i in range(3): for j in ...

  9. 非root用户安装python3

    1.下载源码 wget -c https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz 解压: tar xzf Python-3.7.1.tgz ...

  10. 判断某个元素是否存在于某个 js 数组中

    1.正则表达式 Array.prototype.in_array=function(e){ var r=new RegExp(','+e+','); return (r.test(','+this.j ...