1
我試圖從一個應用程序發送一個字符串到另一個。我會返回一個字符串的響應。在這裏,myhost.com/views是我需要發送字符串值並從中獲得響應的第二個應用程序。但是,當我試圖發送它不執行此代碼。有人可以糾正我在哪裏我錯了嗎?錯誤:目標主機不能爲空,或在參數中設置
下面是我寫的代碼。
public static void sendData(String strval) throws IOException{
String doSend="https://myhost.com/views?strval="+strval;
HttpClient httpclient = new DefaultHttpClient();
try {
System.out.println("inside try");
URIBuilder builder = new URIBuilder();
System.out.println("builder="+builder);
builder.setHost("myhost.com").setPath("/views");
builder.addParameter("strval", strval);
System.out.println("add param,sethost,setpath complete");
URI uri = builder.build();
System.out.println("uri="+uri);
HttpGet httpget = new HttpGet(uri);
System.out.println("httpGet"+httpget);
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine().toString());
if (response.getStatusLine().getStatusCode() == 200) {
String responseText = EntityUtils.toString(response.getEntity());
System.out.println("responseText="+responseText);
httpclient.getConnectionManager().shutdown();
} else {
System.out.println("Server returned HTTP code "
+ response.getStatusLine().getStatusCode());
}
} catch (java.net.URISyntaxException bad) {
System.out.println("URI construction error: " + bad.toString());
}
catch(Exception e){ System.out.println("e.getMessage=>"+e.getMessage()); }
}
代碼運行,直到當我打印例外我看到excep.getMessage() - >
java.lang.IllegalStateException: Target host must not be null, or set in parameters.
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:789)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:414)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
你可以找到類路徑上的jar列表?您需要httpclient jar和httpcore jar。 – roby
問題在於我使用的httpcore jar版本。 版本4.2.1是不兼容的,我使用4.2.3版本,它工作正常,直到 HttpGet httpget = new HttpGet(uri);更新了異常堆棧,因爲我在同一代碼中看到了其他錯誤。 – smiley