Java HttpUrlConnection在本地主机上不工作
这是我的问题:我的Java HttpUrlConnection在本地主机上不工作,java,android,android-studio,Java,Android,Android Studio,这是我的问题:我的HttpUrlConnection无法工作 我创建了一个名为poc android的DNS服务器,它通向一个Apache服务器。我的目标是获取有关设备的一些信息,在urlconnection中使用参数调用服务器,并实现一个数据库 这是我的代码,刚刚在“通道配置”之后停止,在测试之后,似乎OutputStream os=connection.getOutputStream()和/或int-responseCode=connection.getResponseCode()负责此查询
HttpUrlConnection
无法工作
我创建了一个名为poc android的DNS服务器,它通向一个Apache服务器。我的目标是获取有关设备的一些信息,在urlconnection中使用参数调用服务器,并实现一个数据库
这是我的代码,刚刚在“通道配置”之后停止,在测试之后,似乎OutputStream os=connection.getOutputStream()代码>和/或int-responseCode=connection.getResponseCode()
负责此查询的崩溃(它只是停止,从不进入try块)。但是,当目标服务器是一个已知站点时,如https://google.com
或https://youtube.com
,它工作正常,我得到一个响应代码
那么为什么它会在DNS上停止呢
public class HttpQuery {
public void getData(String id ) throws IOException {
Log.d("Debug query","passage getData");
URL url = new URL("https://poc-android");
Log.d("Debug query","lien : "+ url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
Log.d("Debug query","passage connexion");
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "BASIC ...");
connection.setDoOutput(true);
Log.d("Debug query","passage configuration");
String bodyData = "...";
OutputStream os = connection.getOutputStream();
Log.d("Debug query", String.valueOf(os));
os.write(bodyData.getBytes());
os.flush();
os.close();
int responseCode = connection.getResponseCode();
Log.d("Debug query","Response code is: " + responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
this.toResponseResult(connection);
} else {
this.toErrorResult(connection);
System.out.println("Receive error " + responseCode + " when sending request");
}
Log.d("Debug query", "fin query");
}
private @NotNull String readStream(@NotNull InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
public String toResponseResult(@NotNull HttpsURLConnection conn) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
return this.getResponseDataFromStream(in);
}
public String toErrorResult(@NotNull HttpsURLConnection conn) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
return this.getResponseDataFromStream(in);
}
private @NotNull String getResponseDataFromStream(@NotNull BufferedReader in) throws IOException {
String inputLine = "";
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("The content of response is: " + response);
return response.toString();
}
}
如果你明白它为什么停了,我就买它。。。
谢谢 设置读取超时并查看是否超时。