Steps to call the other party’s http interface:
URL url = new URL(path);
1. Open the connection with the url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
2. Set up a general request Attribute
conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
3. Set whether to output to httpUrlConnection and whether to read from httpUrlConnection.
In addition to sending post requests, the most commonly used Http requests are get and post. The get request can obtain a static page, or you can put the parameters after the URL string and pass it to the servlet. The difference between post and get The parameters in the post are not placed in the URL string, but in the body of the http request.
conn.setDoOutput(true); conn.setDoInput(true);
4. Disconnect, it is best to write, disconnect is only cut off when the underlying tcp socket link is idle. If it is being used by other threads, it will not be cut off. If multi-threading is fixed, if you do not disconnect, the number of links will increase until no more information can be sent or received. After writing disconnect, it becomes normal.
conn.disconnect();
Code demonstration:
package com.c; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; /** * @author 包福平 * @QQ:1140913970 */ public class ToInterface { /** * 调用对方接口方法 * @param path 对方或第三方提供的路径 * @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析 */ public static void interfaceUtil(String path,String data) { try { URL url = new URL(path); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 // conn.setRequestMethod("POST"); // //设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 conn.setDoOutput(true); conn.setDoInput(true); //获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); //发送请求参数即数据 out.print(data); //缓冲数据 out.flush(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { System.out.println(str); } //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。 conn.disconnect(); System.out.println("完整结束"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { interfaceUtil("http://api.map.baidu.com/telematics/v3/weather?location=嘉兴 &output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ", ""); // interfaceUtil("http://192.168.10.89:8080/eoffice-restful/resources/sys/oadata", "usercode=10012"); // interfaceUtil("http://192.168.10.89:8080/eoffice-restful/resources/sys/oaholiday", // "floor=first&year=2017&month=9&isLeader=N"); } }
Recommended tutorial: Java tutorial
The above is the detailed content of How to call the other party's interface in java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
