如何使用java调用c#提供的webapi接口

可以使用apache.commons下的一个httpclient的库,c#使用的asp.net下使用webapi对外提供http服务,然后发布在iis服务器上,就可以对外暴露接口了

一,引入apache.commons依赖

二,在你需要调用的接口中编辑请求信息,比如数据格式,编码格式等

废话不多说,直接上代码,不过这里要区分一下,get请求和post请求是有区别的,其次post请求参数的格式也好多种,我这边只列举json格式的;

//get请求
 @GetMapping("/web/get")
    public String getHttp() {
        String responseMsg = "";
        HttpClient httpClient = new HttpClient();

        GetMethod getMethod = new GetMethod("http://192.168.31.124:8086/actionApi/UserInfo/Getsstring");
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
        try {
            httpClient.executeMethod(getMethod);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream in = getMethod.getResponseBodyAsStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while((len=in.read(buf))!=-1){
                out.write(buf, 0, len);
            }
            responseMsg = out.toString("UTF-8");
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放连接
            getMethod.releaseConnection();
        }
        return responseMsg;
    }

//post请求
@PostMapping("/add/user")
    public String postHttp(Student student) throws UnsupportedEncodingException, JsonProcessingException {
        ObjectMapper JSON=new ObjectMapper();
        String responseMsg = "";
        String json=JSON.writeValueAsString(student);
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setContentCharset("UTF-8");
        PostMethod postMethod = new PostMethod("http://192.168.31.124:8086/actionApi/UserInfo/InsertData");
        RequestEntity entity = new StringRequestEntity(json,"application/json", "UTF-8");
        postMethod.setRequestEntity(entity);
        try {
            httpClient.executeMethod(postMethod);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream in = postMethod.getResponseBodyAsStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while((len=in.read(buf))!=-1){
                out.write(buf, 0, len);
            }
            responseMsg = out.toString("UTF-8");
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();
        }
        return responseMsg;
    }


                
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Java调用WebAPI,你可以使用Java中的HttpClient库来发送HTTP请求。首先,你需要引入Apache的HttpClient库。你可以使用Maven或者Gradle来添加依赖。接下来,你需要创建一个HttpClient对象,并设置请求的参数,例如请求的URL、请求方法、请求头等。然后,你可以发送请求并获取响应结果。最后,你可以对响应结果进行解析和处理。以下是一个示例代码: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class Main { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet("http://your-web-api-url"); try { HttpResponse response = httpClient.execute(request); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用HttpClient发送了一个GET请求,并将响应结果输出到控制台。你可以根据实际需求修改请求方法、请求参数等。注意,你需要替换`http://your-web-api-url`为实际的WebAPI的URL。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [如何使用java调用c#提供webapi接口](https://blog.csdn.net/weixin_59244784/article/details/122973567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java处理,调用外系统的 WebAPI(https请求)时,相关知识整理](https://blog.csdn.net/sxzlc/article/details/128878523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EntyIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值