如何使用java.net.URLConnection触发和处理HTTP请求?

如何使用java.net.URLConnection触发和处理HTTP请求?,java,http,httprequest,httpurlconnection,urlconnection,Java,Http,Httprequest,Httpurlconnection,Urlconnection,在这里,经常会问到的用法是什么,而且对它的描述过于简洁 该教程基本上只显示了如何触发GET请求并读取响应。它没有解释如何使用它来执行POST请求、设置请求头、读取响应头、处理cookie、提交HTML表单、上载文件等 那么,我如何使用java.net.URLConnection来触发和处理“高级”HTTP请求呢?首先要声明:发布的代码片段都是基本示例。您需要处理琐碎的IOExceptions和RuntimeExceptions,如NullPointerException,ArrayIndexOu

在这里,经常会问到的用法是什么,而且对它的描述过于简洁

该教程基本上只显示了如何触发GET请求并读取响应。它没有解释如何使用它来执行POST请求、设置请求头、读取响应头、处理cookie、提交HTML表单、上载文件等

那么,我如何使用
java.net.URLConnection
来触发和处理“高级”HTTP请求呢?

首先要声明:发布的代码片段都是基本示例。您需要处理琐碎的
IOException
s和
RuntimeException
s,如
NullPointerException
ArrayIndexOutOfBoundsException
,并与您自己进行关联

如果您是为Android而不是Java开发的,请注意,自从引入API级别28以来,明文HTTP请求就不存在了。我们鼓励您使用
HttpsURLConnection
,但如果确实需要,可以在应用程序清单中启用明文


准备 我们首先需要至少知道URL和字符集。参数是可选的,取决于功能要求

String url = "http://example.com";
String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
String param1 = "value1";
String param2 = "value2";
// ...

String query = String.format("param1=%s&param2=%s", 
     URLEncoder.encode(param1, charset), 
     URLEncoder.encode(param2, charset));
查询参数必须采用
name=value
格式,并由
&
连接。您通常还可以使用指定的字符集来指定查询参数

字符串#format()
只是为了方便起见。当我需要两次以上的字符串连接操作符
+
时,我更喜欢它


使用(可选)查询参数触发请求 这是一项微不足道的任务。这是默认的请求方法

URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...
任何查询字符串都应该使用
连接到URL。标头可能会提示服务器参数的编码方式。如果不发送任何查询字符串,则可以将
Accept字符集
标题保留。如果您不需要设置任何标题,那么您甚至可以使用快捷方式方法

InputStream response = new URL(url).openStream();
// ...
无论哪种方式,如果另一端是,则将调用其方法,并且参数将由提供

出于测试目的,您可以将响应正文打印到stdout,如下所示:

try (Scanner scanner = new Scanner(response)) {
    String responseBody = scanner.useDelimiter("\\A").next();
    System.out.println(responseBody);
}

使用查询参数触发请求 将设置为
true
会隐式地将请求方法设置为POST。web表单的标准HTTP POST类型为
application/x-www-form-urlencoded
,其中查询字符串被写入请求主体

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

try (OutputStream output = connection.getOutputStream()) {
    output.write(query.getBytes(charset));
}

InputStream response = connection.getInputStream();
// ...
注意:当您希望以编程方式提交HTML表单时,不要忘记将任何
元素的
name=value
对放入查询字符串,当然也要将您希望以编程方式“按”的
元素的
name=value
对放入查询字符串(因为这通常在服务器端用于区分是否按下了按钮,如果按下了按钮,则是哪个按钮)

您也可以将获取的连接强制转换为并使用其。但是,如果您试图将该连接用于输出,则仍需要设置为
true

HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
// ...
无论哪种方式,如果另一端是,则将调用其方法,并且参数将由提供


实际触发HTTP请求 您可以使用显式激发HTTP请求,但当您想要获取有关HTTP响应的任何信息(例如使用的响应正文等)时,请求将根据需要自动激发。上面的示例正是这样做的,因此
connect()
调用实际上是多余的


收集HTTP响应信息
  • :
  • 在这里你需要一个角色,必要时先施展

        int status = httpConnection.getResponseCode();
    
  • :


    维持会议 服务器端会话通常由cookie支持。某些web表单要求您登录和/或由会话跟踪。您可以使用API维护cookie。在发送所有HTTP请求之前,您需要准备一个带有的

    // First set the default cookie manager.
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    
    // All the following subsequent URLConnections will use the same cookie manager.
    URLConnection connection = new URL(url).openConnection();
    // ...
    
    connection = new URL(url).openConnection();
    // ...
    
    connection = new URL(url).openConnection();
    // ...
    
    // Gather all cookies on the first request.
    URLConnection connection = new URL(url).openConnection();
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
    // ...
    
    // Then use the same cookies on all subsequent requests.
    connection = new URL(url).openConnection();
    for (String cookie : cookies) {
        connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
    }
    // ...
    
    请注意,这并非在所有情况下都能正常工作。如果失败,那么最好是手动收集和设置cookie头。您基本上需要从登录响应或第一个
    GET
    请求中获取所有
    set cookie
    头,然后将其传递给后续请求

    // First set the default cookie manager.
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    
    // All the following subsequent URLConnections will use the same cookie manager.
    URLConnection connection = new URL(url).openConnection();
    // ...
    
    connection = new URL(url).openConnection();
    // ...
    
    connection = new URL(url).openConnection();
    // ...
    
    // Gather all cookies on the first request.
    URLConnection connection = new URL(url).openConnection();
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
    // ...
    
    // Then use the same cookies on all subsequent requests.
    connection = new URL(url).openConnection();
    for (String cookie : cookies) {
        connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
    }
    // ...
    
    但是,如果事先确实不知道内容长度,那么您可以通过相应地设置来使用分块流模式。这将把HTTP头设置为
    chunked
    ,这将强制以分块的形式发送请求正文。下面的示例将以1KB的分块发送正文

    httpConnection.setChunkedStreamingMode(1024);
    

    用户代理 可能会发生这种情况。服务器端可能正在根据请求头阻止请求。默认情况下,
    URLConnection
    会将其设置为
    Java/1.6.0_19
    ,其中最后一部分显然是JRE版本。您可以按如下方式覆盖此设置:

    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); // Do as if you're using Chrome 41 on Windows 7.
    
    从中使用用户代理字符串


    错误处理 如果HTTP响应代码为
    4nn
    (客户端错误)或
    5nn
    (服务器错误),则您可能需要读取
    HttpURLConnection#getErrorStream()
    ,以查看服务器是否发送了任何有用的错误信息

    InputStream error = ((HttpURLConnection) connection).getErrorStream();
    
    如果HTTP响应代码为-1,则连接和响应处理出现问题。
    HttpURLConnection
    实现在较旧的JRE中,在保持连接活动方面有些问题。您可能希望通过将
    HTTP.keepAlive
    系统属性设置为
    false
    来关闭它。您可以执行此程序在您的申请开始时:

    System.setProperty("http.keepAlive", "false");
    

    上传文件 您通常会对混合发布内容(二进制和字符数据)使用编码。中详细介绍了编码

    如果另一端是,则将调用它的方法,并且部件将由(注意,因此不是
    getParameter()
    等等!)提供
    System.setProperty("http.keepAlive", "false");
    
    String param = "value";
    File textFile = new File("/path/to/file.txt");
    File binaryFile = new File("/path/to/file.bin");
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String CRLF = "\r\n"; // Line separator required by multipart/form-data.
    URLConnection connection = new URL(url).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    
    try (
        OutputStream output = connection.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
    ) {
        // Send normal param.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF).append(param).append(CRLF).flush();
    
        // Send text file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
        writer.append(CRLF).flush();
        Files.copy(textFile.toPath(), output);
        output.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
    
        // Send binary file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        Files.copy(binaryFile.toPath(), output);
        output.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
    
        // End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF).flush();
    }
    
    static {
        TrustManager[] trustAllCertificates = new TrustManager[] {
            new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null; // Not relevant.
                }
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    // Do nothing. Just allow them all.
                }
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    // Do nothing. Just allow them all.
                }
            }
        };
    
        HostnameVerifier trustAllHostnames = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true; // Just allow them all.
            }
        };
    
        try {
            System.setProperty("jsse.enableSNIExtension", "false");
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCertificates, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
        }
        catch (GeneralSecurityException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    int responseCode = httpURLConnection.getResponseCode();
    
    if (responseCode == HttpURLConnection.HTTP_OK) {
    
    package org.boon.utils;
    
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Map;
    
    import static org.boon.utils.IO.read;
    
    public class HTTP {
    
    public static String get(
            final String url) {
    
        Exceptions.tryIt(() -> {
            URLConnection connection;
            connection = doGet(url, null, null, null);
            return extractResponseString(connection);
        });
        return null;
    }
    
    public static String getWithHeaders(
            final String url,
            final Map<String, ? extends Object> headers) {
        URLConnection connection;
        try {
            connection = doGet(url, headers, null, null);
            return extractResponseString(connection);
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    }
    
    public static String getWithContentType(
            final String url,
            final Map<String, ? extends Object> headers,
            String contentType) {
        URLConnection connection;
        try {
            connection = doGet(url, headers, contentType, null);
            return extractResponseString(connection);
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    }
    public static String getWithCharSet(
            final String url,
            final Map<String, ? extends Object> headers,
            String contentType,
            String charSet) {
        URLConnection connection;
        try {
            connection = doGet(url, headers, contentType, charSet);
            return extractResponseString(connection);
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    }
    
    public static String postBody(
            final String url,
            final String body) {
        URLConnection connection;
        try {
            connection = doPost(url, null, "text/plain", null, body);
            return extractResponseString(connection);
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    }
    
    public static String postBodyWithHeaders(
            final String url,
            final Map<String, ? extends Object> headers,
            final String body) {
        URLConnection connection;
        try {
            connection = doPost(url, headers, "text/plain", null, body);
            return extractResponseString(connection);
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    }
    
    
    
    public static String postBodyWithContentType(
            final String url,
            final Map<String, ? extends Object> headers,
            final String contentType,
            final String body) {
    
        URLConnection connection;
        try {
            connection = doPost(url, headers, contentType, null, body);
    
    
            return extractResponseString(connection);
    
    
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    
    
    }
    
    
    public static String postBodyWithCharset(
            final String url,
            final Map<String, ? extends Object> headers,
            final String contentType,
            final String charSet,
            final String body) {
    
        URLConnection connection;
        try {
            connection = doPost(url, headers, contentType, charSet, body);
    
    
            return extractResponseString(connection);
    
    
        } catch (Exception ex) {
            Exceptions.handle(ex);
            return null;
        }
    
    
    }
    
    private static URLConnection doPost(String url, Map<String, ? extends Object> headers,
                                        String contentType, String charset, String body
                                        ) throws IOException {
        URLConnection connection;/* Handle output. */
        connection = new URL(url).openConnection();
        connection.setDoOutput(true);
        manageContentTypeHeaders(contentType, charset, connection);
    
        manageHeaders(headers, connection);
    
    
        IO.write(connection.getOutputStream(), body, IO.CHARSET);
        return connection;
    }
    
    private static void manageHeaders(Map<String, ? extends Object> headers, URLConnection connection) {
        if (headers != null) {
            for (Map.Entry<String, ? extends Object> entry : headers.entrySet()) {
                connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
            }
        }
    }
    
    private static void manageContentTypeHeaders(String contentType, String charset, URLConnection connection) {
        connection.setRequestProperty("Accept-Charset", charset == null ? IO.CHARSET : charset);
        if (contentType!=null && !contentType.isEmpty()) {
            connection.setRequestProperty("Content-Type", contentType);
        }
    }
    
    private static URLConnection doGet(String url, Map<String, ? extends Object> headers,
                                        String contentType, String charset) throws IOException {
        URLConnection connection;/* Handle output. */
        connection = new URL(url).openConnection();
        manageContentTypeHeaders(contentType, charset, connection);
    
        manageHeaders(headers, connection);
    
        return connection;
    }
    
    private static String extractResponseString(URLConnection connection) throws IOException {
    /* Handle input. */
        HttpURLConnection http = (HttpURLConnection)connection;
        int status = http.getResponseCode();
        String charset = getCharset(connection.getHeaderField("Content-Type"));
    
        if (status==200) {
            return readResponseBody(http, charset);
        } else {
            return readErrorResponseBody(http, status, charset);
        }
    }
    
    private static String readErrorResponseBody(HttpURLConnection http, int status, String charset) {
        InputStream errorStream = http.getErrorStream();
        if ( errorStream!=null ) {
            String error = charset== null ? read( errorStream ) :
                read( errorStream, charset );
            throw new RuntimeException("STATUS CODE =" + status + "\n\n" + error);
        } else {
            throw new RuntimeException("STATUS CODE =" + status);
        }
    }
    
    private static String readResponseBody(HttpURLConnection http, String charset) throws IOException {
        if (charset != null) {
            return read(http.getInputStream(), charset);
        } else {
            return read(http.getInputStream());
        }
    }
    
    private static String getCharset(String contentType) {
        if (contentType==null)  {
            return null;
        }
        String charset = null;
        for (String param : contentType.replace(" ", "").split(";")) {
            if (param.startsWith("charset=")) {
                charset = param.split("=", 2)[1];
                break;
            }
        }
        charset = charset == null ?  IO.CHARSET : charset;
    
        return charset;
    }
    
    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {
    
            InputStream requestBody = t.getRequestBody();
            String body = IO.read(requestBody);
            Headers requestHeaders = t.getRequestHeaders();
            body = body + "\n" + copy(requestHeaders).toString();
            t.sendResponseHeaders(200, body.length());
            OutputStream os = t.getResponseBody();
            os.write(body.getBytes());
            os.close();
        }
    }
    
    
    @Test
    public void testHappy() throws Exception {
    
        HttpServer server = HttpServer.create(new InetSocketAddress(9212), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    
        Thread.sleep(10);
    
    
        Map<String,String> headers = map("foo", "bar", "fun", "sun");
    
        String response = HTTP.postBodyWithContentType("http://localhost:9212/test", headers, "text/plain", "hi mom");
    
        System.out.println(response);
    
        assertTrue(response.contains("hi mom"));
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
    
        response = HTTP.postBodyWithCharset("http://localhost:9212/test", headers, "text/plain", "UTF-8", "hi mom");
    
        System.out.println(response);
    
        assertTrue(response.contains("hi mom"));
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
        response = HTTP.postBodyWithHeaders("http://localhost:9212/test", headers, "hi mom");
    
        System.out.println(response);
    
        assertTrue(response.contains("hi mom"));
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
    
        response = HTTP.get("http://localhost:9212/test");
    
        System.out.println(response);
    
    
        response = HTTP.getWithHeaders("http://localhost:9212/test", headers);
    
        System.out.println(response);
    
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
    
    
        response = HTTP.getWithContentType("http://localhost:9212/test", headers, "text/plain");
    
        System.out.println(response);
    
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
    
    
        response = HTTP.getWithCharSet("http://localhost:9212/test", headers, "text/plain", "UTF-8");
    
        System.out.println(response);
    
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
        Thread.sleep(10);
    
        server.stop(0);
    
    
    }
    
    @Test
    public void testPostBody() throws Exception {
    
        HttpServer server = HttpServer.create(new InetSocketAddress(9220), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    
        Thread.sleep(10);
    
    
        Map<String,String> headers = map("foo", "bar", "fun", "sun");
    
        String response = HTTP.postBody("http://localhost:9220/test", "hi mom");
    
        assertTrue(response.contains("hi mom"));
    
    
        Thread.sleep(10);
    
        server.stop(0);
    
    
    }
    
    @Test(expected = RuntimeException.class)
    public void testSad() throws Exception {
    
        HttpServer server = HttpServer.create(new InetSocketAddress(9213), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    
        Thread.sleep(10);
    
    
        Map<String,String> headers = map("foo", "bar", "fun", "sun");
    
        String response = HTTP.postBodyWithContentType("http://localhost:9213/foo", headers, "text/plain", "hi mom");
    
        System.out.println(response);
    
        assertTrue(response.contains("hi mom"));
        assertTrue(response.contains("Fun=[sun], Foo=[bar]"));
    
        Thread.sleep(10);
    
        server.stop(0);
    
    
    }
    
    String html = new JdkRequest("http://www.google.com").fetch().body();
    
    // GET http://google.com?q=baseball%20gloves&size=100
    String response = HttpRequest.get("http://google.com", true, "q", "baseball gloves", "size", 100)
            .accept("application/json")
            .body();
    System.out.println("Response was: " + response);
    
    HttpURLConnection.setFollowRedirects(true); // defaults to true
    
    String url = "https://name_of_the_url";
    URL request_url = new URL(url);
    HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
    http_conn.setConnectTimeout(100000);
    http_conn.setReadTimeout(100000);
    http_conn.setInstanceFollowRedirects(true);
    System.out.println(String.valueOf(http_conn.getResponseCode()));
    
    HttpURLConnection.setFollowRedirects(true); // defaults to true
    
    String url = "https://name_of_the_url"
    URL request_url = new URL(url);
    HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
    http_conn.setConnectTimeout(100000);
    http_conn.setReadTimeout(100000);
    http_conn.setInstanceFollowRedirects(true);
    http_conn.setDoOutput(true);
    PrintWriter out = new PrintWriter(http_conn.getOutputStream());
    if (urlparameter != null) {
       out.println(urlparameter);
    }
    out.close();
    out = null;
    System.out.println(String.valueOf(http_conn.getResponseCode()));
    
    OkHttpClient client = new OkHttpClient();
    
    Request request = new Request.Builder()
          .url(url)
          .build();
    
    Response response = client.newCall(request).execute();
    
    urlConnection.setDoOutput(true);
    
    // GET
    HttpResponse response = HttpRequest
        .create(new URI("http://www.stackoverflow.com"))
        .headers("Foo", "foovalue", "Bar", "barvalue")
        .GET()
        .response();
    
    int statusCode = response.statusCode();
    String responseBody = response.body(HttpResponse.asString());
    
    module com.foo.bar {
        requires jdk.incubator.httpclient;
    }