使用 java 访问 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4185320/
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-08-14 12:48:07  来源:igfitidea点击:

hitting a URL using java

javahttpurl

提问by aksci

Possible Duplicates:
How to send HTTP request in java?
How to use java.net.URLConnection to fire and handle HTTP requests?

可能的重复:
如何在 Java 中发送 HTTP 请求?
如何使用 java.net.URLConnection 来触发和处理 HTTP 请求?

using java, how do i hit any url?

使用java,我如何点击任何网址?

for instance, opening of http://www.xyz.com/node1in a browser will tell xyz.com that node1 is hit. so in this java program (which sends sms text say 'node1' in example above embedded in the url itself to a sms gateway server)

例如,在浏览器中打开http://www.xyz.com/node1会告诉 xyz.com node1 被命中。所以在这个java程序中(它在上面的例子中发送sms文本说'node1'嵌入在url本身中到一个sms网关服务器)

how do i achieve it without opening any browser or using servlet.

我如何在不打开任何浏览器或使用 servlet 的情况下实现它。

回答by Mark Peters

You can use an HttpURLConnection.

您可以使用HttpURLConnection

But using it directly is overkill if you just want to load the URL in question. This guideshows you how to open a URL.

但是,如果您只想加载有问题的 URL,直接使用它是过度的。 本指南向您展示如何打开 URL。

Basically it boils down to:

基本上可以归结为:

URL url = new URL("http://www.xyz.com/node1");
URLConnection conn = url.openConnection();
conn.connect();
//...

回答by Richard H

The simplest way is to use URL http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html. For more advanced/flexible URL fetching you could use HttpClient http://hc.apache.org/httpclient-3.x/

最简单的方法是使用 URL http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html。对于更高级/灵活的 URL 获取,您可以使用 HttpClient http://hc.apache.org/httpclient-3.x/