80,472
社区成员




[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头信息无效";
}
//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;
}
}
// 定义一个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"));
}
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);
}
}