Gab*_*ich 10 java httpconnection
我知道关于这个话题有几个问题但我没有找到任何答案.
我正在尝试打开与本地服务器的连接,但我一直拒绝连接.
我有服务器运行,我测试了与浏览器的连接和一个名为Postman的谷歌应用程序,它的工作原理.
打开连接时失败,好像没有任何东西要连接.或者有什么东西阻止连接?我测试了防火墙和防病毒软件,没有运气.
在Postman中测试URL返回一个用户应该...
如果我用" http://www.google.com " 替换网址它工作正常.
这是我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Gabriel
*/
public class HttpConnection {
public HttpConnection() {
}
public void makeRequest() throws MalformedURLException, IOException {
String url = "http://localhost:8000/users/1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
con.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8,es;q=0.6");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Host", "localhost:8000");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我会大胆猜测可能是什么问题。可能存在 IPv4/IPv6 问题。
如果是这样,这里有两种可能的解决方案
java.net.preferIPv4Stack=true| 归档时间: |
|
| 查看次数: |
7335 次 |
| 最近记录: |