1. First URL restURL = new URL(url)
; The url is the target interface address that needs to be adjusted, and the URL class is java.net.* category under.
2, setRequestMethod("POST")
; The request method has two values to choose from, one is GET and the other is POST, select the corresponding request method
3. setDoOutput(true);setDoInput(true)
;
setDoInput(): // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内, 因此需要设为true, 默认是false; setDoOutput(): // 设置是否从httpUrlConnection读入,默认情况下是true;
4. setAllowUserInteraction();allowUserInteraction
If true, user interaction is allowed (such as pop-up This URL is checked within the context of a validation dialog box.
5. The query in the following code is transmitted in the form of "attribute=value". If there are multiple queries, it is passed in the form of "attribute=value&attribute=value". It is passed to the server and let the server handle it by itself.
6, close();
Create a stream to write or read the return value. Remember to close the stream after creation.
Example tutorial:
package com.c; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class RestUtil { public String load(String url,String query) throws Exception { URL restURL = new URL(url); /* * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类 HttpURLConnection */ HttpURLConnection conn = (HttpURLConnection) restURL.openConnection(); //请求方式 conn.setRequestMethod("POST"); //设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true); conn.setDoOutput(true); //allowUserInteraction 如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。 conn.setAllowUserInteraction(false); PrintStream ps = new PrintStream(conn.getOutputStream()); ps.print(query); ps.close(); BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line,resultStr=""; while(null != (line=bReader.readLine())) { resultStr +=line; } System.out.println("3412412---"+resultStr); bReader.close(); return resultStr; } public static void main(String []args) {try { RestUtil restUtil = new RestUtil(); String resultString = restUtil.load( "http://192.168.10.89:8080/eoffice-restful/resources/sys/oaholiday", "floor=first&year=2017&month=9&isLeader=N"); } catch (Exception e) { // TODO: handle exception System.out.print(e.getMessage()); } } }
Recommended tutorial:Java tutorial
The above is the detailed content of Example tutorial of calling interface in java. For more information, please follow other related articles on the PHP Chinese website!

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool