【发布时间】:2014-12-18 16:58:49
【问题描述】:
我有一个 C# 客户端,Web 服务必须接收证书标头。我不知道如何将标头添加到 Web 服务。这是我的代码:
// Define a SOAP header by deriving from the SoapHeader base class.
// The header contains just one string value.
public class MyHeader : SoapHeader
{
public string MyValue;
}
public partial class Service: System.Web.UI.Page
{
MyWebService ESClient = new MyWebservice();
private static String TAG_CERTIFICATE = "MxIFZjFCBFa...";
protected void Page_Load(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
CredentialCache cache = new CredentialCache();
MyHeader header = new MyHeader();
// Populate the values of the SOAP header.
header.MyValue = TAG_CERTIFICATE;
//HOW CAN I ADD THIS CERTIFICATE TO MY ESCLIENT WEB SERVICE?????
//remSolSal is the response of the Web Service
remSolSal response= new remSolSal();
//remDatSol is the method of the Web Service and getRemSolEnt the parameters that I send in another function
response = ESClient.remDatSol(getRemSolEnt());
}
XML 模型证书:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sol="url...">
<soapenv:Header>
<certificate>
JgsJSP6Ql8f........
</certificate>
另外,我有一个 Java 客户端,它可以完成我在 C# 中必须做的事情。它使用 this 函数添加标题,其中 paramName 是“证书”,paramContent 是值“JgsJSP6Ql8f........”:
/**
* This method adds a custom header to message.
*
* @param paramName
*/
private static void addHeaderParam(String paramName, String paramContent) {
try {
List<Header> headers = new ArrayList<Header>();
Header dummyHeader = new Header(new QName(TARGETNAMESPACE,
paramName), paramContent, new JAXBDataBinding(String.class));
headers.add(dummyHeader);
// client side:
((BindingProvider) port).getRequestContext().put(
Header.HEADER_LIST, headers);
} catch (JAXBException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
查看
getRemSolEnt()返回的对象的属性。看看它是否有标题的属性。 -
@JohnSaunders 他需要使用 WCF API 来执行此操作,它不能只分配给请求对象。
-
@moarboilerplate:如果他使用的是
SoapHeader,那么他使用的是Web Reference,而不是Service Reference,所以没有WCF。 -
没错,但似乎这只是一些测试代码,试图实现他的要求......
-
您使用的是服务参考还是网络参考?
标签: c# xml web-services wcf soap