我从这个链接http服务器:http://www.rgagnon.com/javadetails/java-have-a-simple-http-server.html(第一个例子)Java的HTTP服务器客户端的例子
我运行它的它的优良工作。然后我用下面的小代码作为客户端与服务器进行通信:
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
public class Main {
public static void main(String argv[]) throws ClientProtocolException, IOException
{
String url = "127.0.0.1/test";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
System.out.println("http response = "+response.toString());
}
}
我执行它,但我得到这个异常:
Exception in thread "main" java.lang.IllegalStateException: Target host is null
at org.apache.http.util.Asserts.notNull(Asserts.java:46)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at httpClient.Main.main(Main.java:20)
有一个想法,我怎么能解决这个问题?
尝试打开浏览器,通过'http:// 127.0.0.1/info' – Berger