我正在尝试将SOAP请求发送到使用WSSE、和UsernameToken进行身份验证的web服务。示例查询如下(屏蔽机密数据):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:kas="http://webservice.com">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>abc</wsse:Username>
<wsse:CustomField>123</wsse:CustomField>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<kas:method1>
<!--Optional:-->
<method1>
<!--Optional:-->
<queryNo>12345678901</queryNo>
</method1>
</kas:method1>
</soapenv:Body>
</soapenv:Envelope>
我使用WSE3.0生成了一个代理类,问题是我收到错误:"Object reference not set to a instance of an object“。我的C#代码中有问题的部分如下:
queryNoSorguType q = new queryNoSorguType();
string query_parameter = query_no;
q.queryNo = query_parameter;
ResultType[] r = new ResultType[10];
UsernameToken token = new UsernameToken("abc", "123",PasswordOption.SendPlainText);
//mWebService.SetClientCredential<UsernameToken>(token);
//Policy webServiceClientPolicy = new Policy();
mWebService.RequestSoapContext.Security.Tokens.Add(token);
//mWebService.SetPolicy(webServiceClientPolicy);
//r = mWebService.documentQuerybyQueryNo(q);
System.Data.DataTable outputDataTable = new System.Data.DataTable();
//System.Data.DataRow outRow = outputDataTable.Rows.Add();
//outRow["field1"] = r;
output = outputDataTable;
我通过系统地注释掉我的代码部分,找到了有问题的部分。我对web服务和C#非常不熟悉,实际上我是在蓝棱镜中实现的。尽管这个程序可以开箱即用SOAP web服务,但不幸的是,它本身并不支持SOAP头。
SOAP请求在SOAP UI中运行良好,并且在Blue Prism中没有编译器错误。我试着按照手册和网络上的说明添加标题,但不起作用。如果你能给我指出正确的方向,我将不胜感激。
编辑在Visual Studio2017中编写、编译控制台应用程序后,我得到了以下错误。据我所知,它没有头部的定义。
Unhandled Exception: System.Web.Services.Protocols.SoapHeaderException: MustUnderstand headers:[{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security] are not understood
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at WebService.queryByQueryNo(queryNoQueryType queryByQueryNo1) in C:\Users\user\source\repos\ConsoleApp1\ConsoleApp1\Web References\WebService\Reference.cs:line 1533
at ConsoleApp1.Program.Main(String[] args) in C:\Users\user\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 33
发布于 2018-06-04 03:14:45
我决定使用一种不同的方法,暂时不再尝试使用代理类,因为存在与其相关的问题。利用这个链接上的答案:Client to send SOAP request and receive response,我在一些定制之后提出了我自己的解决方案。
在Visual Studio中编写代码并对其进行测试后,将其移植到Blue Prism中非常容易。
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Net;
using System.IO;
using System.Xml.Linq;
namespace WebService
{
class Program
{
/// <summary>
/// Execute a Soap WebService call
/// </summary>
public static string Execute(string queryNo)
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:customNAMESPACE=""http://webservice.com"">
<soapenv:Header>
<wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<wsse:UsernameToken>
<wsse:Username>USER</wsse:Username>
<wsse:CustomField>CODE</wsse:CustomField>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<customNAMESPACE:QueryByQueryNo>
<!--Optional:-->
<QueryByQueryNo>
<!--Optional:-->
<queryNo>" + queryNo + @"</queryNo>
</QueryByQueryNo>
</customNAMESPACE:QueryByQueryNo>
</soapenv:Body>
</soapenv:Envelope>");
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
return soapResult;
}
}
}
/// <summary>
/// Create a soap webrequest to [Url]
/// </summary>
/// <returns></returns>
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://webservice.com/webservice?wsdl");
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
static void Main(string[] args)
{
if (args.Length == 0 || args.Length > 1)
{
System.Console.WriteLine("Please provide a query no");
System.Console.WriteLine("Usage: WebService.exe 3523423333");
return;
}
string output, XMLresponse;
try
{
XMLresponse = Execute(args[0]);
output = "Successful query";
XmlDocument xml = new XmlDocument();
xml.LoadXml(XMLresponse); // suppose that str string contains the XML data. You may load XML data from a file too.
XmlNodeList resultCodeList = xml.GetElementsByTagName("resultCode");
XmlNodeList resultNoList = xml.GetElementsByTagName("resultNo");
int i = 0;
var OutputTable = new DataTable();
OutputTable.Columns.Add("Result Code", typeof(string));
OutputTable.Columns.Add("Result No", typeof(string));
foreach (XmlNode xn in resultCodeList)
{
Console.WriteLine(resultCodeList[i].InnerText + " " + resultNoList[i].InnerText);
OutputTable.Rows.Add(resultCodeList[i].InnerText, resultNoList[i].InnerText);
i++;
}
}
catch (System.Net.WebException exc)
{
Console.WriteLine("HTTP POST request failed!");
output = "!POST request failed!";
}
}
}
}
发布于 2018-05-31 03:42:23
我认为是xml结构在您使用
<wsse:Security
wsse没有定义,我知道您在同一行中定义了它,但为什么不尝试将它放到文档中,如下所示
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:kas="http://webservice.com" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>abc</wsse:Username>
<wsse:CustomField>123</wsse:CustomField>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
https://stackoverflow.com/questions/50430398
复制相似问题