Android调用webservice里面的soaphead校验问题,解决方案有没有

azhe0929 2011-10-13 07:30:22
昨天的时候遇上一个问题,一直到今天都没解决,其实问题也不是很大,就是传不过去,也上网搜过很多资料都没有解决方案,在加的群里也问了,都不太清楚。,可以帮我分析下问题的所在吗
我把代码发上来,您看下
这个是C#编写的webservice里面的客户端要调用的 有soapheader的 一个返回json数据的方法
[SoapHeader("MySoapHeader",Direction=SoapHeaderDirection.InOut)]
[SoapRpcMethod, WebMethod(MessageName = "Json")]//具体方法中也要指定rpc方式
public string GetUserDate(string sqlstr)
{
string msg = string.Empty;
if (MySoapHeader != null)
{
if (MySoapHeader.IsValid(MySoapHeader.Name, MySoapHeader.Password, out msg))
{

sqlconn sqlconn = new sqlconn();
DataTable selDt = sqlconn.getSelDate(sqlstr);
String json = ToJson.DataTableToJson("json", selDt);
return json;
}
else
{
return msg;//返回错误信息
}

}
else
return "Soap头信息无效";


}

在开始我定义了一个继承了SoapHeader的Mysoapheader,接下来是定义带有soaphead的方法

下面是一个soaphead类的定义
//SoapHead类定义

public class MySoapHeader:System.Web.Services.Protocols.SoapHeader
{
private string name;
private string password;

public string Name
{
get { return name; }
set { name = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public MySoapHeader() { }
public MySoapHeader(string nName, string nPassword)
{
name = nName;
password = nPassword;
}
/// <summary>
/// 校验soaphead信息
/// </summary>
/// <param name="name">用户名</param>
/// <param name="password">密码</param>
/// <param name="msg">返回信息</param>
/// <returns>是否正确</returns>
public bool IsValid(string name, string password,out string msg)
{
msg = "";
try
{
if (name == "user" || password == "password")
{
return true;
}
else
{
msg = "您无权使用此服务";
return false;
}
}
catch
{
msg = "您无权使用此服务";
return false;
}
}


webservice写好了,下面是客户端去调用,客户端是Android项目,使用java编写的
用了一个轻量级的jar包 ksoap2

以上是添加了一个对象
下面是添加soaphead
// 定义一个soapheader
Element[] header = new Element[1];
header[0] = new Element().createElement(_NAMESPACE, "MySoapHeader");
Element Name = new Element().createElement(_NAMESPACE, "Name");
Name.addChild(Node.TEXT, "user");
header[0].addChild(Node.ELEMENT, Name);
Element Password = new Element().createElement(_NAMESPACE, "Password");
Password.addChild(Node.TEXT, "password");
header[0].addChild(Node.ELEMENT, Password);
// 使用soap的版本ver11
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
/* 发送请求 */
envelope.headerOut = header;
envelope.bodyOut = rpc;

// 注意这里要设置为false,否则读不出数据
envelope.dotNet = false;
//设置请求参数
envelope.setOutputSoapObject(rpc);
AndroidHttpTransport ht = new AndroidHttpTransport(_URL);
ht.debug = true;
/*调用*/
try
{
ht.call(_SOAP_ACTION, envelope);
}
catch (Exception e)
{
return "IOException:" + e.getMessage();
}
// 用Bodyin读取xml body里面的数据
SoapObject result = (SoapObject) envelope.bodyIn;
Log.i("g",String.valueOf(result.getProperty("JsonResult")));
return String.valueOf(result.getProperty("JsonResult"));
}


下面是调用返回数据,如果不考虑soaphead验证不加这个head的话,我传递数据是没有问题的

可是我加了这个head之后,返回的body信息里面就是
,也就是说soaphead并没有从客户端赋值到服务器里面,Mysoapheader为null,
不知道是什么原因啊,很困惑,很多地方都试过了都不行,比如 客户端和服务端的命名原因啊,网上的资料国内也蛮少,国外的好像也没有,另外我在webservice里面建一个Asp.net应用程序,然后引用webservice,用.net的客户端去调用.net的服务端这时候是成功的,能够把值传给Mysoapheader 。进行校验,并成功返回json数据。
麻烦帮我看下,是哪里出了问题呢?
...全文
561 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
大谷子 2013-11-27
  • 打赏
  • 举报
回复
楼主能不能共享一下您使用的jar包 ksoap2
azhe0929 2011-10-18
  • 打赏
  • 举报
回复
自己搞定了,哈哈,原来是在.net端 不能指定用RPC模式,而且envelope.dotNet = true;自己顶自己的努力!!
azhe0929 2011-10-14
  • 打赏
  • 举报
回复
自己顶下,求给力回答!!
azhe0929 2011-10-14
  • 打赏
  • 举报
回复
后来我改了下,webservice里面的(直接把我服务端贴了)
using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/HelloWorld")]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
//这是对于SoapAction的处理,问题不在这里
//[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[SoapRpcService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public class Service : System.Web.Services.WebService
{
//创建Soapheader
public MySoapHeader mySoapHeader;

public Service()
{

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[SoapRpcMethod, WebMethod(MessageName = "HelloWorld")]//具体方法中也要指定rpc方式
public string Hello()
{
return "Hello World";
}
[SoapHeader("mySoapHeader", Direction = SoapHeaderDirection.In)]
[SoapRpcMethod, WebMethod(MessageName = "Json")]//具体方法中也要指定rpc方式
public string GetUserDate(string sqlstr)
{
string msg = string.Empty;
if (mySoapHeader != null)
{

if (mySoapHeader.IsValid(out msg))
{

sqlconn sqlconn = new sqlconn();
DataTable selDt = sqlconn.getSelDate(sqlstr);
String json = ToJson.DataTableToJson("json", selDt);
return json;
}
else
{
return msg;//返回错误信息
}

}
else
return "Soap头信息无效";

}
[SoapRpcMethod, WebMethod(MessageName = "compress")]//具体方法中也要指定rpc方式
public Byte[] NewGetUserDate(string sqlstr)
{
sqlconn sqlconn = new sqlconn();
DataTable selDt = sqlconn.getSelDate(sqlstr);
String strjson = ToJson.DataTableToJson("json", selDt);

//字符串转换
byte[] bytejson = System.Text.Encoding.Unicode.GetBytes(strjson);
GzipCompress gzipcompress = new GzipCompress();
byte[] compressedbytejson = gzipcompress.Compress(bytejson);
return compressedbytejson;
// return Convert.FromBase64CharArray(compressedbytejson);

}
}
//SoapHead类定义

public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
private string name;
private string password;

public string Name
{
get { return name; }
set { name = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
/// <summary>
/// 校验soaphead信息
/// </summary>
/// <param name="name">用户名</param>
/// <param name="password">密码</param>
/// <param name="msg">返回信息</param>
/// <returns>是否正确</returns>
public bool IsValid(string name, string password, out string msg)
{
msg = "";
try
{
if (name == "user" || password == "password")
{
return true;
}
else
{
msg = name+password+"无权使用此服务";
return false;
}
}
catch
{
msg =name+password+ "您无权使用此服务";
return false;
}
}
public bool IsValid(out string msg)
{
return IsValid(name, password, out msg);
}



}



前面的那个//[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[SoapRpcService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)],这时候,Mysoaphead那个不为空了,也就是被初始化了,但是里面的值依然为空,依然没有值
azhe0929 2011-10-14
  • 打赏
  • 举报
回复

返回的body信息是 无权使用,也就是
if (name == "user" || password == "password")
{
return true;
}
else
{
msg = "您无权使用此服务";
return false;
}
}
else里面的那个 您无权使用
cnmahj 2011-10-14
  • 打赏
  • 举报
回复
返回的body信息里面就是
什么啊?怎么这里也没写全?

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧