HttpComponents - Connect to different Port

2019-08-09 10:23发布

I have to write an android client, in which i should use HttpComponents to connect to a specific Server on Port 8080. For now, all i've found, was the Examplecode from the Apache-site, which is nearly perfect for what i need, except the Port it connects to:

    if (isSet)
    {
        throw new IOException("Hostname or Port are not set!");
    }
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(serverURL + ":" + serverPort + "/maps");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity != null)
    {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1)
        {

        }
    }

Is there any way to change the Port? Any help would be appreciated.

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-09 10:49

I know a little about HttpComponents, but I find an example code from Apache-site, which may help you. You can try to modify the code like below for your problem.

        HttpHost target = new HttpHost("issues.apache.org", 443, "https");
        HttpGet request = new HttpGet("/");
        CloseableHttpResponse response = httpclient.execute(target, request);
查看更多
登录 后发表回答