Yesterday I met a problem, until today no solution, in fact, the problem is not great, that is, do not pass over, also found a lot of information on the Internet are not solutions, plus a group where also asked, are not clear. Can help me analyze the problem lies right
I put the code made up, you look
This is
method returns json data written in C # webservice client inside there soapheader to invoke the
[SoapHeader ("MySoapHeader", Direction = SoapHeaderDirection.InOut)]
[SoapRpcMethod, WebMethod (MessageName = "Json")] / / rpc specific method should specify ways
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 ;/ / return an error message
}
}
else
return "Soap header information is invalid";
} In the beginning, I defined a inherited SoapHeader of Mysoapheader, followed by the method defined with soaphead of
Here is the definition of a soaphead class
/ / SoapHead class definition
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;
}
/ / /
/ / / Check soaphead information
/ / /
/ / / username
/ / / password
/ / / return information
/ / /Correct
public bool IsValid (string name, string password, out string msg)
{
msg = "";
try
{
if (name == "user" | | password == "password")
{
return true;
}
else
{
msg = "You have no right to use this service";
return false;
}
}
catch
{
msg = "You have no right to use this service";
return false;
}
}
webservice written, the following is to call the client, the client is the Android project, prepared using
javaJar with a lightweight package ksoap2
These are added to an object
Here is added soaphead
/ / define a 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);.
/ / Use soap version ver11
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope.VER11);
/ * Send the request * /
envelope.headerOut = header;
envelope.bodyOut = rpc;
/ / Note that this should be set to false, otherwise it can not reproduce the data
envelope.dotNet = false;
/ / Set the request parameters
envelope.setOutputSoapObject (rpc);
AndroidHttpTransport ht = new AndroidHttpTransport (_URL);
ht.debug = true;
/ * Call * /
try
{
ht.call (_SOAP_ACTION, envelope);
}
catch (Exception e)
{
return "IOException:" + e.getMessage ();
}
/ / Read the data inside
xml body with BodyinSoapObject result = (SoapObject) envelope.bodyIn;
Log.i ("g", String.valueOf (result.getProperty ("JsonResult")));
return String.valueOf (result.getProperty ("JsonResult"));
}
Here is the call to return the data, if you do not consider soaphead verification without the head, I pass the data is no problem
But after I added the head, body of information which is returned
, That does not assign soaphead from the client to the server inside, Mysoapheader is null,
I do not know what the reasons are, ah, very confused, many places have tried will not work, such as the reasons for naming the client and service side of the, ah, the Internet is also very little information on domestic and foreign seems to be no, and I built inside an webservice Asp.net application, and then reference webservice, with. net client to call this time the server net is successful, the value can be passed Mysoapheader. For verification, and successfully return json data.
Trouble to help me see, where is the problem?
Reply:
Their own roof, seeking to force the answer! !
Reply:
Body of information which is returned
What ah? How did write all here?
Reply:
Body is not entitled to use the information returned, which is
if (name == "user" | | password == "password")
{
return true;
}
else
{
msg = "You have no right to use this service";
return false;
}
}
else inside that you are not entitled to use
Reply:
Later, I changed the next, webservice inside (directly to my server posted)
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")]
/ / To allow the use of ASP.NET AJAX call this Web service from a script, please cancel the downlink comments.
/ / [System.Web.Script.Services.ScriptService]
[WebServiceBinding (ConformsTo = WsiProfiles.None)]
/ / This is for SoapAction handling, the problem is not here
/ / [SoapDocumentService (RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[SoapRpcService (RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public class Service: System.Web.Services.WebService
{
/ / Create Soapheader
public MySoapHeader mySoapHeader;
public Service ()
{
/ / If the component design, uncomment the following line
/ / InitializeComponent ();
}
[SoapRpcMethod, WebMethod (MessageName = "HelloWorld")] / / rpc specific method should specify ways
public string Hello ()
{
return "Hello World";
}
[SoapHeader ("mySoapHeader", Direction = SoapHeaderDirection.In)]
[SoapRpcMethod, WebMethod (MessageName = "Json")] / / rpc specific method should specify ways
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 ;/ / return an error message
}
}
else
return "Soap header information is invalid";
}
[SoapRpcMethod, WebMethod (MessageName = "compress")] / / rpc specific method should specify ways
public Byte [] NewGetUserDate (string sqlstr)
{
sqlconn sqlconn = new sqlconn ();
DataTable selDt = sqlconn.getSelDate (sqlstr);
String strjson = ToJson.DataTableToJson ("json", selDt);
/ / String conversion
byte [] bytejson = System.Text.Encoding.Unicode.GetBytes (strjson);
GzipCompress gzipcompress = new GzipCompress ();
byte [] compressedbytejson = gzipcompress.Compress (bytejson);
return compressedbytejson;
/ / Return Convert.FromBase64CharArray (compressedbytejson);
}
}
/ / SoapHead class definition
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;}
}
/ / /
/ / / Check soaphead information
/ / /
/ / / username
/ / / password
/ / / return information
/ / /Correct
public bool IsValid (string name, string password, out string msg)
{
msg = "";
try
{
if (name == "user" | | password == "password")
{
return true;
}
else
{
msg = name + password + "has no right to use this service";
return false;
}
}
catch
{
msg = name + password + "You have no right to use this service";
return false;
}
}
public bool IsValid (out string msg)
{
return IsValid (name, password, out msg);
}
}
In front of the / / [SoapDocumentService (RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[SoapRpcService (RoutingStyle = SoapServiceRoutingStyle.RequestElement)], this time, Mysoaphead that is not empty, that is initialized, but the value is still empty inside, still has no value
Reply:
Get their own, ha ha, it was not specified by net client RPC model, but envelope.dotNet = true;. Their top their own efforts! !
Reply:
Landlord can share some jar package you are using ksoap2
No comments:
Post a Comment