简体   繁体   English

通过Java中的IP地址将HTTP GET请求发送到目标

[英]Send HTTP GET request to destination by IP address in java

I have an Apache web server launched on another computer in my local network, that have a .php file in its root folder, so I can send my requests from browser using addresses like "192.168.1.5/connect.php?param1=value1&param2=value2". 我在本地网络的另一台计算机上启动了一个Apache Web服务器,该服务器的根文件夹中有一个.php文件,因此我可以使用“ 192.168.1.5/connect.php?param1=value1&param2= value2”。 I really need to do that from my java code (by using HttpUrlConnection), but by some reason it works only with url, not with the IP address, I mentioned above. 我确实需要从我的Java代码中执行此操作(通过使用HttpUrlConnection),但是由于某种原因,它仅适用于url,不适用于IP地址,如上所述。 Is there a way to make it work with IP instead of url? 有没有办法使其与IP而不是url一起使用?

HttpUrlConnection可以完美地用于主机名HTTP连接和IP地址HTTP连接:

HttpURLConnection conn = (HttpURLConnection) new URL("http://192.168.1.5/connect.php?param1=value1&param2=value2").openConnection();

Please read and understand the URL syntax as described here https://www.ietf.org/rfc/rfc1738.txt 请阅读并理解此处描述的URL语法https://www.ietf.org/rfc/rfc1738.txt

Having said that, the http scheme allows hostnames or ip addresses. 话虽如此,http方案允许主机名或ip地址。

http://192.168.1.5/connect.php?param1=value1&param2=value2 http://192.168.1.5/connect.php?param1=value1&param2=value2

is a valid URL. 是有效的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM