org.apache.commons.httpclient 访问需要验证的webservice的一些问题
来源:互联网 发布:网络热门歌曲 编辑:程序博客网 时间:2024/06/05 21:15
1、httpclient验证问题
webservice需要验证时,直接发送请求会返回 HTTP/1.1 401 Unauthorized 错误
这时候需要设置:
Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
注意password为明文
2、Unsupported xstream 错误
这个需要进行设置请求类型,一般请求如下设置,此为post请求:
byte[] b = soapRequestData.getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/xop+xml; charset=UTF-8; type=\"text/xml\"");
postMethod.setRequestEntity(re);
3、Unbuffered entity enclosing request can not be repeated.问题
一般来说,webservice需要验证时,在httpclient请求之前需要加上上面的设置,然后使用上面的访问进行post访问时会发生此错误
此时需要将设置改为如下:
StringRequestEntity requestEntity = new StringRequestEntity(soapRequestData,"application/xop+xml; charset=UTF-8; type=\"text/xml\"","UTF-8");
postMethod.setRequestEntity(requestEntity);
因此使用httpclient访问需要验证的webservice时,具体代码如下
public static void post() throws Exception {HttpClient httpClient = new HttpClient();//post请求内容String soapRequestData ="postbody";// 构造HttpClient的实例Credentials defaultcreds = new UsernamePasswordCredentials("username", "password"); httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds); //webservice服务请求路径String url = "";PostMethod postMethod = new PostMethod(url);//使用系统提供的默认的恢复策略postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler()); StringRequestEntity requestEntity = new StringRequestEntity(soapRequestData,"application/xop+xml; charset=UTF-8; type=\"text/xml\"","UTF-8"); postMethod.setRequestEntity(requestEntity); // 执行postMethodint statusCode = httpClient.executeMethod(postMethod);// HttpClient对于要求接受后继服务的请求,System.out.println("statusCode is "+statusCode);if (statusCode != HttpStatus.SC_OK) {System.err.println("Method failed: "+ postMethod.getStatusLine());}// 读取内容byte[] responseBody = postMethod.getResponseBody();// 处理内容System.out.println(new String(responseBody,"utf-8"));}
- org.apache.commons.httpclient 访问需要验证的webservice的一些问题
- org.apache.commons.httpclient.HttpClient的使用
- org.apache.commons.httpclient.HttpClient的使用
- org.apache.http.client.HttpClient和org.apache.commons.httpclient.HttpClient的区别
- 什么是httpclient?& Httpclient使用前提----下载org.apache.commons.httpclient,导入Httpclient的jar包
- org.apache.commons.httpclient.HttpClient请求乱码问题
- 使用org.apache.commons.httpclient
- 对于 org.apache.commons.beanutils.BeanUtils 的一些学习
- org.apache.commons.httpclient.HttpClient请求
- org.apache.commons.httpclient.HttpClient;解析xml
- org.apache.commons.httpclient.HttpClient 学习
- HTTPClient调用---org.apache.commons.httpclient
- org.apache.commons.lang3.xwork.StringUtils找不到的问题
- SSL自签名的实现类org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory
- android org.apache.commons.httpclient 调试
- org.apache.axis2.deployment.DeploymentException: org/apache/commons/httpclient/HttpException
- commons-httpclient-3.1.jar的一些用处
- commons-httpclient-3.1.jar的一些用处
- onCreateOptionsMenu(Menu menu)方法与2. nPrepareOptionsMenu(Menu menu)方法的区别
- apache 二级域名设置
- Android URL openConnection无法打开链接
- 为Android Gallery添加手势翻页
- 老子的软件之道 - 道篇 12 表示层与处理层
- org.apache.commons.httpclient 访问需要验证的webservice的一些问题
- CSDN新版下载频道介绍之四——资源评分评论及积分日志功能改进
- Uninstall ParetoLogic Filecure with Special Uninstaller, Special Uninstaller Review
- IBATIS 数据库字符集转换(中西文转换)
- Android中的Java反射机制应用
- android给ScrollView中显示多个控件 --公司项目 在左边栏 加入滚条
- POJ1112 Team Them Up! 连通图 染色 DP
- 让Android Gallery可以识别BMP图片文件
- Tomcat 6.x 使用连接池