我必须编写一个Android客户端,我应该使用HttpComponents连接到端口8080上的特定服务器。 目前,我发现的只是来自Apache网站的Examplecode,它几乎是我需要的,除了它连接到的端口:
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)
{
}
}
有没有办法改变港口? 任何帮助将不胜感激。
答案 0 :(得分:0)
我对HttpComponents了解不多,但我从Apache-site找到了an example code,这可能对你有所帮助。你可以尝试修改下面的代码来解决你的问题。
HttpHost target = new HttpHost("issues.apache.org", 443, "https");
HttpGet request = new HttpGet("/");
CloseableHttpResponse response = httpclient.execute(target, request);