Home > Java > JavaBase > body text

Example tutorial of calling interface in java

王林
Release: 2019-11-13 11:50:51
Original
4672 people have browsed it

Example tutorial of calling interface in java

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;
Copy after login

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());

            }

        }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template