Httpclient:Target host is not specified ,目标主机不唯一
在httpclient 端发送Json格式数据到服务端,是一个简单的分页查询,但是总是报这个错误,求大神指点一下
1.Httpclient端代码如下:
①String invokeCal = HttpClientPool.invokePost("http.car.data",params);//http,car.data是调用的地址,从配置文件读取
http.car.data="http://192.168.1.xx:端口号/项目名/方法名",这个大神都懂
invokePost(String,Map<String,String > params)这个方法代码如下:
王亚 15:19:30
public static String invokePost(String address, Map<String, String> params) throws IOException {
String responseBody = "";
try {
HttpPost httpget = new HttpPost(address);
List<NameValuePair> entity = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
entity.add(new BasicNameValuePair(key, params.get(key)));
}
httpget.setEntity(new UrlEncodedFormEntity(entity, HTTP.UTF_8));
logger.debug("Executing request " + httpget.getRequestLine());
// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
// return entity != null ?
// convertStreamToString(entity.getContent()) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
responseBody = httpclient.execute(httpget, responseHandler);
logger.debug("----------------------------------------");
logger.debug(responseBody);
} finally {
// httpclient.close();
}
return responseBody;
}
②服务端代码:
@RestController
@RequestMaping ("/xxxxxx")
public class xxxxx(){
@RequestMaping ("/page")
@ResponseBody
Object page(接收各种参数){
}
}