客户端发送SOAP请求并接收响应。

8 浏览
0 Comments

客户端发送SOAP请求并接收响应。

尝试创建一个C#客户端(将作为Windows服务开发),该客户端向Web服务发送SOAP请求并获取结果。

从这个问题中,我看到了这段代码:

protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
{
    var wr = WebRequest.Create(soapMessage.Uri);
    wr.ContentType = "text/xml;charset=utf-8";
    wr.ContentLength = soapMessage.ContentXml.Length;
    wr.Headers.Add("SOAPAction", soapMessage.SoapAction);
    wr.Credentials = soapMessage.Credentials;
    wr.Method = "POST";
    wr.GetRequestStream().Write(Encoding.UTF8.GetBytes(soapMessage.ContentXml), 0, soapMessage.ContentXml.Length);
    return wr;
}
public interface ISoapMessage
{
    string Uri { get; }
    string ContentXml { get; }
    string SoapAction { get; }
    ICredentials Credentials { get; }
}

看起来不错,有人知道如何使用它吗?这是否是最佳实践?

0
0 Comments

客户端发送SOAP请求并接收响应的问题出现的原因是缺少正确的SOAPAction头部。解决方法是在代码中添加正确的SOAPAction头部。

具体的解决方法如下所示:

1. 在CreateWebRequest方法中,使用webRequest.Headers.Add("SOAPAction", "http://tempuri.org/.....")来添加正确的SOAPAction头部。

2. 将webRequest.Headers.Add(@"SOAPAction", "SomeAction")用于添加正确的SOAPAction头部。

以下是整理的文章:

在使用.NET 4.0 C#时,可以通过以下方法发送SOAP请求并接收响应,而无需使用WSDL或代理类。具体的代码如下:

class Program
{
    /// 
    /// 执行SOAP WebService调用
    /// 
    public static void Execute()
    {
        HttpWebRequest request = CreateWebRequest();
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"
            
              
                
              
            ");
        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);
            }
        }
    }
    
    /// 
    /// 创建SOAP WebRequest到[Url]
    /// 
    /// 
    public static HttpWebRequest CreateWebRequest()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:56405/WebService1.asmx?op=HelloWorld");
        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)
    {
        Execute();
    }
}

然而,如果没有正确设置SOAPAction头部,可能会遇到以下错误信息:'soap' is an undeclared prefix. Line 2, position 18. 为了解决这个问题,我们可以使用以下方法来设置正确的SOAPAction头部:

webRequest.Headers.Add("SOAPAction", "http://tempuri.org/.....");

或者

webRequest.Headers.Add(@"SOAPAction", "SomeAction");

以上是关于如何发送SOAP请求并接收响应的解决方法。希望能对你有所帮助。

0
0 Comments

问题的出现原因:

该问题的出现是因为客户端需要发送SOAP请求并接收响应,但是现有的代码中没有实现这个功能。

解决方法:

为了解决这个问题,可以按照以下步骤进行操作:

1. 创建一个SOAP请求的XML文档。

2. 创建一个HTTP请求,并设置请求的URL、动作和请求头。

3. 将SOAP请求的XML文档插入到HTTP请求中。

4. 发送异步的HTTP请求,并等待响应。

5. 从完成的HTTP请求中获取响应,并读取响应内容。

下面是具体的代码示例:

using System.Xml;
using System.Net;
using System.IO;
public static void CallWebService()
{
    var _url = "http://xxxxxxxxx/Service1.asmx";
    var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";
    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
    // 开始异步调用web请求。
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
    // 挂起此线程,直到调用完成。你可能想在这里做一些有用的事情,比如更新你的UI。
    asyncResult.AsyncWaitHandle.WaitOne();
    // 从完成的web请求中获取响应。
    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    {
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResult = rd.ReadToEnd();
        }
        Console.Write(soapResult);        
    }
}
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 XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelopeDocument = new XmlDocument();
    soapEnvelopeDocument.LoadXml(
    @"
        
            
                12
                32
            
        
    ");
    return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}

希望以上的解决方法能帮助你解决问题。

0
0 Comments

问题出现的原因是发送的SOAP请求中的soap:header中包含了一个token,但是服务器没有正确识别该token。解决方法是确保在发送请求时正确设置了token,并且服务器能够正确处理该token。

以下是一个可能的解决方法示例代码:

public async Task<string> CreateSoapEnvelope()

{

string soapString = @"<?xml version=""1.0"" encoding=""utf-8""?>

<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">

<soap:Header>

<token>your_token_here</token>

</soap:Header>

<soap:Body>

<HelloWorld xmlns=""http://tempuri.org/"" />

</soap:Body>

</soap:Envelope>";

HttpResponseMessage response = await PostXmlRequest("your_url_here", soapString);

string content = await response.Content.ReadAsStringAsync();

return content;

}

public static async Task<HttpResponseMessage> PostXmlRequest(string baseUrl, string xmlString)

{

using (var httpClient = new HttpClient())

{

var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");

httpContent.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");

return await httpClient.PostAsync(baseUrl, httpContent);

}

}

在这个示例代码中,我们在soap:header中添加了一个token,并在CreateSoapEnvelope方法中设置了正确的token值。确保将"your_token_here"替换为真实的token值。

这个示例代码使用了HttpClient来发送请求,而不是已经过时的WebResponse,因此它是更可靠的解决方法。

通过使用上述示例代码,您应该能够正确发送SOAP请求并接收到服务器的响应。

0