Java 错误:目标主机不能为null,也不能在参数中设置
我正在尝试将字符串从一个应用程序发送到另一个应用程序。我将返回一个字符串响应。这里,myhost.com/views是我需要发送字符串值并从中获取响应的第二个应用程序。但当我试图发送时,它并没有执行此代码。有人能纠正我哪里错了吗 下面是我写的代码Java 错误:目标主机不能为null,也不能在参数中设置,java,http,url,Java,Http,Url,我正在尝试将字符串从一个应用程序发送到另一个应用程序。我将返回一个字符串响应。这里,myhost.com/views是我需要发送字符串值并从中获取响应的第二个应用程序。但当我试图发送时,它并没有执行此代码。有人能纠正我哪里错了吗 下面是我写的代码 public static void sendData(String strval) throws IOException{ String doSend="https://myhost.com/views?strval="+strval;
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()->
此代码无法将其识别为有效的
URI
,因为它缺少http
。我添加了以下内容来解析代码:
builder.setScheme("http");
你能在类路径上找到JAR列表吗?您需要httpclient jar和httpcore jar。问题在于我使用的httpcore jar版本。版本4.2.1是不兼容的,我使用了版本4.2.3,在HttpGet-HttpGet=new-HttpGet(uri)之前它工作得很好;在同一代码中看到其他错误时更新了异常堆栈。
builder.setScheme("http");