HttpComponents - Connect to different Port

那年仲夏 提交于 2019-12-11 04:39:19

问题


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:


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


来源:https://stackoverflow.com/questions/8113723/httpcomponents-connect-to-different-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!