c# - 无法从 C# 控制台应用程序中的 SOAP 1.2 Web 请求获得响应
问题描述
我无法使用 C# 代码访问 SOAP 1.2 Web 服务。我遵循了这个例子中的代码,Web Service SOAP Call
它对我不起作用。我的 Web 服务 URL 在 SOAPUI 中有效,但在 C# 代码中无法得到响应。我收到“500 内部错误”。状态显示为“ProtocolError”。
我从我的 Visual Studio 2017 编辑器运行它。
using System;
using System.IO;
using System.Net;
using System.Xml;
namespace testBillMatrixConsole
{
class Program
{
static void Main(string[] args)
{
string _url = @"https://service1.com/MSAPaymentService/MSAPaymentService.asmx";
var _action = @"http://schemas.service1.com/v20060103/msapaymentservice/AuthorizePayment";
try
{
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
catch (WebException ex)
{
throw;//ex.Message.ToString();
}
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(@"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:msap=""http://schemas.service1.com/v20060103/msapaymentservice""><soap:Body><msap:AuthorizePayment><!--Optional:--><msap:clientPaymentId>?</msap:clientPaymentId><msap:amount>?</msap:amount><msap:method>?</msap:method><!--Optional:--><msap:parameters><!--Zero or more repetitions:--><msap:PaymentParameter><!--Optional:--><msap:Name>?</msap:Name><!--Optional:--><msap:Value>?</msap:Value></msap:PaymentParameter></msap:parameters></msap:AuthorizePayment></soap:Body></soap:Envelope>");
return soapEnvelopeDocument;
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
}
解决方案
推荐阅读
- java - 使用 feign 进行慢速并行调用
- node.js - Express.js,如果我的参数值在某个值的某处包含冒号,我会在值的前面加上冒号
- c++ - c/c++是否定义有符号整数除法的结果:负极除以负1(INT_MIN/-1)
- python - 在 tkinter 中无法打开的窗口的问题
- python - 在 on_message (discord.py) 中运行命令
- java - 按 Jpa Query 的部分主键过滤
- javascript - 如何使用 Promise 数数
- amazon-web-services - 用于翻译客户端的 aws-sdk-js 导致错误“无法读取未定义的属性 'byteLength'”
- magento2 - 如何向 Magento2 ajax 搜索建议请求添加更多参数?
- xamarin - Xamarin RadioButton IsChecked 在出现完成后由其他 RadioButtonGroup 重置