[英]Connection timed out only for wikmedia api on server but works on local
以下代碼段在過去三年中一直有效,但是突然之間,它僅在服務器中引發了連接超時,但在localhost中按預期工作。 任何意見 ?
public String getWikiContent(String query) {
StringBuilder builder = new StringBuilder();
String path = "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=1&explaintext=1&titles=" + query + "&format=json&redirects";
try {
URL url = new URL(path);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestProperty("Content-Type",
"application/json");
if (urlConn.getResponseCode() != 200) {
throw new IOException(urlConn.getResponseMessage());
}
InputStream is = urlConn.getInputStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = buff.readLine()) != null) {
builder.append(line);
}
}catch (IOException e){
e.printStackTrace();
}
return builder.toString();
}
由於某些原因,這是網絡問題,只需添加代理服務器即可解決。
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url,port));
URLConnection urlConn = url.openConnection(proxy);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.