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();
}
}
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();
}
}
相关文章推荐
- java中用httpClient请求service时bug解决方法(httpGet)
- Java Web开发8__HTTP GET请求、POST请求的区别, 重写 service() 方法
- java中用httpClient请求service,httpPost方法示例
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
- java apache commons HttpClient发送get和post请求的学习整理(转)
- HttpClient的HttpGet请求乱码问题解决
- java apache commons HttpClient发送get和post请求的学习整理
- Android http 请求——AsyncHttpClient集成【解决重定向问题、setEnableRedirects 方法异常、线程中调用异常问题】
- java httpclient发送post get 请求
- Java httpclient请求,解决乱码问题
- 请求的资源不支持 http 方法“GET” 解决
- java apache commons HttpClient发送get和post请求的学习整理
- TIdHTTP get参数带中文解决方法--请求报文
- Flex中HTTPService请求参数中存在中文,后台获取为乱码的解决方法
- java发送post和get请求(1)---HttpClient 方式
- httpclient发送post/get请求,Java后台怎么接受
- Java爬虫入门简介(一) —— HttpClient请求及其使用方法
- okHttpClient异步同步的get和post请求方法
- java apache commons HttpClient发送get和post请求的学习整理