• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

java调用c#webapi代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

上次我们写的.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/

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#Excel或表格插件发布时间:2022-07-13
下一篇:
c#扫描可疑文件(找到木马)(简)转发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap