2

I'm trying to use the web api of µTorrent from my localhost with java. This works but sometimes I get an error in this method.

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;
}

And the error I get is on this statement:

httpclient.execute(targetHost, httpget, localcontext);
org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:2222 [/127.0.0.1] failed: Connection refused: connect
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)

Somebody who can help me with this or give me some insights? Thank you in advance.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • I would think that µTorrent is not always responsive. Perhaps it has a limited number of threads to answer connections. Have you tried connecting to it several times in a row using something like `curl` or `wget`? – RealSkeptic Feb 08 '15 at 18:05
  • Well. In my program I test if µTorrent is opened. Is this not the case I open it and add the torrent. When µTorrent is already opened there is no error, but when I open it and add the torrent there is. – user2257668 Feb 10 '15 at 19:43
  • Then yes, it probably takes it some time to set up the web interface connection. – RealSkeptic Feb 10 '15 at 22:16

0 Answers0