.NET Soap对Web服务的请求 - 动作没有发生

时间:2013-12-09 10:50:59

标签: c# asp.net web-services soap

我已经搜索并发现了很多关于这个主题的内容,但我只是没有让它发挥作用。我的请求从asmx页面返回显示web方法(操作)的HTML,但不执行操作,这基本上返回true / false。

我使用的是我的方法提供的相同的SOAP 1.1。而且,当我使用提供的“调用”按钮测试功能时,它工作得很好。但我真的需要在幕后调用这个函数,所以我不能像这个按钮一样使用HTTP Post。有什么想法吗?

网站的App_Code目录中的

.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

[WebService(Namespace = "http://www.xxx.org/")]

public class VacationSickPayoutLogger : System.Web.Services.WebService
{

    [WebMethod]
    public string RunVacationSickPayoutWS()
    {
        return "True";
    }
}

来自asmx文件的代码:

<%@ WebService Language="C#" CodeBehind="~/App_Code/VacationSickPayoutLogger.cs" Class="VacationSickPayoutLogger" %>

调用网络方法的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        string soap =
        @"<?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:Body>
            <RunVacationSickPayoutWS xmlns=""http://www.xxx.org/"" />
          </soap:Body>
        </soap:Envelope>";

        var _url = "http://localhost/HR/VacationSickPayoutLogger.asmx";
        var _action = "\"http://www.xxx.org/RunVacationSickPayoutWS\"";

        System.Xml.XmlDocument soapEnvelopeXml = CreateSoapEnvelope(soap);
        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();
            }
            Response.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 System.Xml.XmlDocument CreateSoapEnvelope(string soap)
    {
        System.Xml.XmlDocument soapEnvelop = new System.Xml.XmlDocument();
        soapEnvelop.LoadXml(soap);
        return soapEnvelop;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(System.Xml.XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

0 个答案:

没有答案