android下身份验证方式调用webservice
在企业开发领域,webservice还是经常被用到的服务体系,因为他对安全事务支持都比较好。
有时候,我们就需要在android下调用后端的webservice服务,因为在内部网络环境下,所有需要basic身份验证。
一般情况下,我们会用soap包来访问,但是soap包虽然封装的比较好,但是一旦出错很难找到原因。下面我们介绍一种简单的方式,通过http client post来访问webservice;
首先,我们把soap请求拼装成xml字符串,如下:
String soapRequestData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
+ " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
+ " <soap12:Body>"
+ " <GetAPACShippingPackage xmlns=\"xx/\">"
+ " <GetAPACShippingPackageRequest>"
+ " <TrackCode>123</TrackCode>"
+ " <Version>123</Version>"
+ " <APIDevUserID>123</APIDevUserID>"
+ " <APIPassword>123</APIPassword>"
+ " <APISellerUserID>123</APISellerUserID>"
+ " <MessageID>123</MessageID>"
+ " </GetAPACShippingPackageRequest>"
+ " </GetAPACShippingPackage>" + "</soap12:Body>"
+ " </soap12:Envelope>";
创建postMethod:
PostMethod postMethod = new PostMethod(
"http://xx?wsdl");
声明他是一个soap请求:
StringRequestEntity requestEntity = new StringRequestEntity(soapRequestData,"application/soap+xml; charset=UTF-8; type=\"text/xml\"","UTF-8");
postMethod.setRequestEntity(requestEntity);
加上basic身份认证:
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(new AuthScope("host", 80, AuthScope.ANY_REALM),
new UsernamePasswordCredentials("username", "password"));
httpClient.getParams().setAuthenticationPreemptive(true);
最后,像正常的http请求一下,调用起来:
int statusCode = httpClient.executeMethod(postMethod);
if(statusCode == 200) {
System.out.println("调用成功!");
String soapResponseData = postMethod.getResponseBodyAsString();
System.out.println(soapResponseData);
}
else {
System.out.println("调用失败!错误码:" + statusCode);
}
这里的返回值就是webservice返回的xml,需要我们解析这个xml文件来得到数据。
android下身份验证方式调用webservice的更多相关文章
- IIS下的身份验证方式管理
设置.查看身份验证方式 #导航到某站点下: cd IIS:\Sites\DemoSite\DemoApp #启用站点test01下的Windows身份验证 Set-WebConfigurationPr ...
- asp.net中常用的几种身份验证方式
转载:http://www.cnblogs.com/dinglang/archive/2012/06/03/2532664.html 前言 在B/S系统开发中,经常需要使用"身份验证&q ...
- MySql8.0+全新身份验证方式
我们在安装MySql8.0+的版本时MySql将会询问我们是否选择全新的身份验证方式,如下图 ⒈第一个是MySql推荐我们使用的强密码加密模式来进行身份验证 MySql8支持基于SHA256改进的更强 ...
- 第15课-数据库开发及ado.net-数据库介绍,主键,外键,启动数据库,数据库身份验证方式,建表,分离数据库
第15课-数据库开发及ado.net 数据库介绍,主键,外键,启动数据库,数据库身份验证方式,建表,分离数据库 1. 学习方法 2. 多涨见识 3. 比自己强的人一起,学习更强:比自己更聪明的人 ...
- ASP.net的身份验证方式有哪些?
[转] ASP.net的身份验证方式有哪些?分别是什么原理? Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 ...
- Azure Service Bus 中的身份验证方式 Shared Access Signature
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- 使用ajax和urlconnection方式调用webservice服务
<html> <head> <title>使用ajax方式调用webservice服务</title> <script> var xhr = ...
- Windows XPSP3通过网络级身份验证方式连接Windows Server 2008远程桌面
远程桌面大大方便了大家的日常管理工作,Windows Server 2008同样秉承这一优秀特性,并引入网络级身份验证(NLA)作为远程桌面连接的默认身份验证方式. 网络级身份验证 (NLA) 是一种 ...
- Java调用WebService方法总结(9,end)--Http方式调用WebService
Http方式调用WebService,直接发送soap消息到服务端,然后自己解析服务端返回的结果,这种方式比较简单粗暴,也很好用:soap消息可以通过SoapUI来生成,也很方便.文中所使用到的软件版 ...
随机推荐
- HDOJ 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- javascript设计模式-迭代器模式(Iterator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 服务端发送xml请求java代码示例
/** * */ package com.autoyol.pay.cmb.core; import java.io.ByteArrayOutputStream; import java.io.IOEx ...
- MySQL查看表占用空间大小(转)
MySQL查看表占用空间大小(转) //先进去MySQL自带管理库:information_schema //自己的数据库:dbwww58com_kuchecarlib //自己的表:t_carmod ...
- isMobile 一个简单的JS库,用来检测移动设备
点这里 github地址:https://github.com/kaimallea/isMobile Example Usage I include the minified version of t ...
- Dev 统计GridControl界面上当前选中的一行的值
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChang ...
- Junit单元测试学习笔记二
我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4. 一. 包含必要地Package ...
- C++运算符重载——重载二元运算符
1.重载二元操作符的方法 二元运算符又称为双目运算符,即需要2个操作数的运算符,例如 + - * / 等. 运算符重载可以分为3种方式:类的非静态成员函数.类的友元函数.普通函数. 例如有 2 个操作 ...
- Things about single men that women hate
Things about single men that women hate为何你俘获不了女神的心?If you listen in to a group of single women talki ...
- Spring框架学习之第4节
从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...