Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对Web服务的.NET Soap请求-操作未发生_C#_Asp.net_Web Services_Soap - Fatal编程技术网

C# 对Web服务的.NET Soap请求-操作未发生

C# 对Web服务的.NET Soap请求-操作未发生,c#,asp.net,web-services,soap,C#,Asp.net,Web Services,Soap,我已经搜索并找到了很多关于这个话题的东西,但我只是没能让它发挥作用。我的请求从显示web方法(操作)的asmx页面返回HTML,但不执行该操作,该操作基本上返回true/false 我使用的是我的方法提供的相同的SOAP 1.1。而且,当我使用“Invoke”按钮测试功能时,只要它工作正常。但是我真的需要在幕后调用这个函数,所以我不能像这个按钮那样使用HTTPPOST。有什么想法吗 网站App_代码目录中的.cs代码: using System; using System.Collections

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

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

网站App_代码目录中的.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" %>

调用web方法的代码:

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);
        }
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
串肥皂=
@"
";
var_url=”http://localhost/HR/VacationSickPayoutLogger.asmx";
var\u action=“\”http://www.xxx.org/RunVacationSickPayoutWS\"";
System.Xml.XmlDocument soapEnvelopeXml=CreateSoapEnvelope(soap);
HttpWebRequest webRequest=CreateWebRequest(_url,_action);
插入SOAPEnvelopeInToWebRequest(SOAPEnvelopeExML,webRequest);
//开始对web请求的异步调用。
IAsyncResult asyncResult=webRequest.BeginGetResponse(null,null);
//挂起此线程,直到调用完成。您可能希望
//在这里做一些有用的事情,比如更新你的UI。
asyncResult.AsyncWaitHandle.WaitOne();
//从已完成的web请求中获取响应。
字符串soapResult;
使用(WebResponse WebResponse=webRequest.EndGetResponse(asyncResult))
{
使用(StreamReader rd=newstreamreader(webResponse.GetResponseStream()))
{
soapResult=rd.ReadToEnd();
}
Response.Write(soapResult);
}
}
私有静态HttpWebRequest CreateWebRequest(字符串url,字符串操作)
{
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”;
返回webRequest;
}
私有静态System.Xml.XmlDocument CreateSoapEnvelope(字符串soap)
{
System.Xml.XmlDocument soapEnvelope=new System.Xml.XmlDocument();
LoadXml(soap);
返回信封;
}
私有静态void InsertSoapEnvelopeIntoWebRequest(System.Xml.XmlDocument SoapEnvelopeExml,HttpWebRequest webRequest)
{
使用(Stream-Stream=webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(流);
}
}