Java http 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3619106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:41:05  来源:igfitidea点击:

Java http connection

javahttphttps

提问by user323101

I want setup a http connection to send request and get the response in an stand alone java application, can any one help me how can i proceed with this????

我想设置一个 http 连接来发送请求并在一个独立的 Java 应用程序中获得响应,有人可以帮助我如何继续吗????

采纳答案by Mike G

HttpURLConnection connection = null;
    try {
        URL url = new URL("www.google.com");
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
                    // do something with the input stream here

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        if(null != connection) { connection.disconnect(); }
    }

回答by Pablo Santa Cruz

You can use URLConnectionclass bundled with standard Java (since JDK 1.0!), or a higher level HTTP client such as Apache's HTTPCLIENTwhich will provide, in addition to plain HTTP, higher level components like cookies, standard headers and more.

您可以使用与标准 Java(自 JDK 1.0 起!)或更高级别的 HTTP 客户端(例如Apache 的 HTTPCLIENT)捆绑在一起的URLConnection类,除了普通的 HTTP 之外,它还会提供更高级别的组件,如 cookie、标准标头等

回答by Bruno

A couple of answers have already pointed out Apache HTTP Client, but they link to version 3.x, which is no longer maintained. You should use version 4, which has a slightly different API, if you want to use this library: http://hc.apache.org/httpcomponents-client-4.0.1/index.html

一些答案已经指出 Apache HTTP 客户端,但它们链接到 3.x 版,该版本不再维护。如果你想使用这个库,你应该使用版本 4,它有一个稍微不同的 API:http: //hc.apache.org/httpcomponents-client-4.0.1/index.html