我对Web服务缺乏经验,我正在努力让这个工作基于MSDN上的读数和stackoverflow中的读数。显然,我仍然没有得到它,因为我仍然无法使它工作,所以我正在寻求帮助。
以下是我一直在做的以及我需要做的事情:
我有一个示例请求和一个代理类,我需要使用C#向WS发送请求
<soap:Header>
<AppDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.example.com/websvc">
<AppName>Application1</AppName>
</AppDetails>
<userDetails xmlns='http://schemas.example.com/mysoap'>
<USER xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://schemas.example.com/user_schema'>
<userid>12345</userid>
<cid>100</cid>
</USER>
</userDetails>
</soap:Header>
<soap:Body>
<myRequest_getReport xmlns="http://schemas.example.com/websvc" docID="9999"/>
</soap:Body>
我在通过Fiddler创建请求方面取得了巨大的成功,因为它很直接,我只需要填写值,但我已经努力了一周,以便在我的代码中使用它。
这是代理类代码:
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/mysoap")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.example.com/mysoap", IsNullable = false)]
public partial class userDetails : System.Web.Services.Protocols.SoapHeader
{
private System.Xml.XmlElement any_u;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement Any
{
get
{
return this.any_u;
}
set
{
this.any_u = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/infrastructure/user_schema")]
public partial class USER
{
private uint userid_u;
private uint cid_u;
/// <remarks/>
public uint userid
{
get
{
return this.userid_u;
}
set
{
this.userid_u = value;
}
}
/// <remarks/>
public uint cid
{
get
{
return this.cid_u;
}
set
{
this.cid_u = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/websvc")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.example.com/websvc", IsNullable = false)]
public partial class AppDetails : System.Web.Services.Protocols.SoapHeader
{
private string appName_u;
/// <remarks/>
public string AppName
{
get
{
return this.appName_u;
}
set
{
this.appName_u = value;
}
}
}
这是我的代码:
static void Main(string[] args)
{
myRequest req_server = new myRequest("myremoteserver", null); //connects to the server
// User
XmlElement UserIdentity = UserDetailsHeader();
req_server.userIdentityValue = new userDetails();
req_server.userIdentityValue.Any = UserIdentity;
// Application
//XmlElement appDataVal = AppDataHeader();
req_server.AppDataValue = new AppDetails();
req_server.AppDataValue.AppName = "Application1";
// Construct the request
myRequest_getReport docRequest = new myRequest_getReport();
}
private static XmlElement UserDetailsHeader()
{
USER myUser = new USER();
XmlSerializer xs = new XmlSerializer(typeof(USER));
MemoryStream ms = new MemoryStream(1024);
myUser.userid = 12345;
myUser.cid = 100;
xs.Serialize(ms, myUser);
ms.Position = 0;
XmlDocument xd = new XmlDocument();
xd.Load(ms);
return xd.DocumentElement;
}
private static XmlElement AppDataHeader()
{
AppDetails myApp = new AppDetails();
XmlSerializer xs = new XmlSerializer(typeof(AppDetails));
MemoryStream ms = new MemoryStream(1024);
myApp.AppName = "Application1";
xs.Serialize(ms, myApp);
ms.Position = 0;
XmlDocument xd = new XmlDocument();
xd.Load(ms);
return xd.DocumentElement;
}
正如您在UserDetailsHeader()中看到的,我能够在USER中设置userid和cid的值。我也创建了AppDataHeader()但后来无法使用它,因为我后来发现我不必使用它,但无论如何我都把它包含在这里以备不时之需。
我将main()中的UserDetailsHeader()设置为UserIdentity,但是当我执行代码时,会显示一条异常,说“未定义UserDetails”。显然我在这里缺少一些非常重要的东西,我想知道它是什么。
你能指点我正确的方向吗?我现在很困惑,我甚至不确定我的代码是否有意义。 UserDetailsHeader中的代码基于我在C#中如何创建SOAP头请求的读数,我从MSDN文章中得到了这个,我现在找不到。