您的位置:首页 > 理论基础 > 计算机网络

java中用httpClient请求service时bug解决方法(httpGet)

2013-12-27 16:19 651 查看
通过http方式请求客户公司的接口,出现的几个问题以及解决方法:

1.java.lang.NoClassDefFoundError: org/apache/http/params/SyncBasicHttpParams

at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:161)

是因为httpcore-4.1-alpha1.jar包没有导入,导入后异常解除

2.java.lang.VerifyError: Cannot inherit from final class

可能包有冲突,http请求需要的包如下:

commons-logging-1.1.1.jar

commons-codec-1.4.jar

httpclient-4.1.2.jar

httpcore-4.1-alpha1.jar(我这边是因为httpcore-4.0.jar和httpcore-4.1-alpha1.jar冲突了,删掉版本低的)

问题已解决

3.Exception in thread "main" java.lang.IllegalStateException: Target host must not be null, or set in parameters.

出现这个问题可能是你请求的路径不完整,完整如下:必须加上http://

HttpGet httppost = new HttpGet("http://test.service.cc");

以上是我遇到的问题,在百度搜索不到什么答案,谷歌搜索有一个好的网站:http://stackoverflow.com/

你可以搜到自己类似的问题并得到解答

附上下载jar包的网站:http://www.findjar.com/index.x

再附上我的简单测试代码

/**

* 方法描述:

* @auth:danni

* @date:2013-12-27 下午03:35:53

* @param args

* @return:void

*/

public static void main(String[] args) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet httpget= new HttpGet("http://test.service.cc");

httppost.setHeader("Authorization", "5c74c30a35c2b6fdc96bf8409ce66008");//将授权添加到请求头(Header)的 Authorization 属性中

System.out.println("executing request " + httpget.getRequestLine());

HttpResponse response = null;

try {

response = httpClient.execute(httpget);

int code = response.getStatusLine().getStatusCode();

if (code == 200) {

String rev = EntityUtils.toString(response.getEntity());// 返回json格式:

// {"id":

// "27JpL~j4vsL0LX00E00005","version":

// "abc"}

System.out.println("TestHttpGet2.main()" + rev);

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: