Java 使用µ时拒绝连接;通过本地主机以编程方式发送Torrent web api
我正在尝试使用java从本地主机获得的µTorrent的web api。这是可行的,但有时我会在这个方法中出错Java 使用µ时拒绝连接;通过本地主机以编程方式发送Torrent web api,java,httphostconnectexception,Java,Httphostconnectexception,我正在尝试使用java从本地主机获得的µTorrent的web api。这是可行的,但有时我会在这个方法中出错 public String[] connectToWebAPI() { String guid = null; String token = null; String[] tokenAndGuid = new String[2]; targetHost = new HttpHost("127.0.0.1", 2222, "http"); Cr
public String[] connectToWebAPI()
{
String guid = null;
String token = null;
String[] tokenAndGuid = new String[2];
targetHost = new HttpHost("127.0.0.1", 2222, "http");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
try
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
localcontext = new HttpClientContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
CookieStore cookieStore = new BasicCookieStore();
localcontext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://127.0.0.1:2222/gui/");
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
EntityUtils.consumeQuietly(response.getEntity());
httpget = new HttpGet("http://127.0.0.1:2222/gui/token.html");
response = httpclient.execute(targetHost, httpget, localcontext);
HttpEntity e = response.getEntity();
InputStream is = e.getContent();
StringWriter sw = new StringWriter();
IOUtils.copy(is, sw);
sw.flush();
sw.close();
is.close();
//<html><div id='token' style='display:none;'>gzB9zbMru3JJlBf2TbmwwklESgXW2hD_caJfFLvNBjmaRbLZ3kNGnSHrFlIAAAAA</div></html>
String t = sw.toString();
//Get token out of html
int start = "<html><div id='token' style='display:none;'>".length();
int end = t.indexOf("</div></html>");
token = t.substring(start,end);
EntityUtils.consumeQuietly(response.getEntity());
for(Cookie cookie : localcontext.getCookieStore().getCookies())
{
if(cookie.getName().equals("GUID"))
guid = cookie.getValue();
}
httpclient.close();
}
catch (Exception e)
{
tokenAndGuid[0] = "error";
return tokenAndGuid;
}
tokenAndGuid[0] = token;
tokenAndGuid[1] = guid;
return tokenAndGuid;
}
org.apache.http.conn.HttpHostConnectException:连接到127.0.0.1:2222[/127.0.0.1]失败:连接被拒绝:连接
位于org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
位于org.apache.http.impl.conn.poolghttpclientconnectionmanager.connect(poolghttpclientconnectionmanager.java:318)
位于org.apache.http.impl.execchain.MainClientExec.buildRoute(MainClientExec.java:363)
位于org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
位于org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
位于org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
有人能帮我做这件事或者给我一些见解吗?
提前谢谢。我认为µTorrent并不总是有响应。也许它有有限数量的线程来应答连接。您是否连续多次尝试使用类似于
curl
或wget
的方法连接到它。在我的程序中,我测试是否打开了µTorrent。这不是我打开它并添加torrent的情况。当µTorrent已经打开时,没有错误,但当我打开它并添加Torrent时,会有错误。然后是的,可能需要一些时间来设置web界面连接。
httpclient.execute(targetHost, httpget, localcontext);