Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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服务的调用完成?_C#_Web Services_Soap - Fatal编程技术网

C# 如何等待对web服务的调用完成?

C# 如何等待对web服务的调用完成?,c#,web-services,soap,C#,Web Services,Soap,我正在尝试从web服务获取FedAuth Cookie。但是,在我从web服务获得结果之前,另一个方法GetFedAuthCookieFromSOAPResponse正在执行,传递给它的参数正在变为null。因此,我得到了一个例外: startIndex不能大于字符串的长度。参数名称:startIndex 如何等待从web服务获得soap响应,然后执行方法GetFedAuthCookieFromSOAPResponse static void Main(string[] args)

我正在尝试从web服务获取FedAuth Cookie。但是,在我从web服务获得结果之前,另一个方法
GetFedAuthCookieFromSOAPResponse
正在执行,传递给它的参数正在变为
null
。因此,我得到了一个例外:

startIndex不能大于字符串的长度。参数名称:startIndex

如何等待从web服务获得soap响应,然后执行方法
GetFedAuthCookieFromSOAPResponse

static void Main(string[] args)
        {
            string url = "https://server/site";
            string soapResponse = GetSOAPResponse(url, "http://site/server/exapmle.asmx");
            Console.WriteLine(GetFedAuthCookieFromSOAPResponse(soapResponse));
            Console.WriteLine("Press ENTER to close program");
            Console.ReadLine();
        }
        internal static string GetSOAPResponse(string url, string serviceURL)
        {
            using (WebClient client = new WebClient())
            {
                string request = "<?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><GetFedAuthCookie xmlns=\"http://tempuri.org/\"><requestUrl>" + url + "</requestUrl><userName></userName><password></password></GetFedAuthCookie></soap:Body></soap:Envelope>";
                client.Headers.Add(HttpRequestHeader.ContentType, "text/xml; charset=utf-8");
                client.Headers.Add("SOAPAction", "http://tempuri.org/GetFedAuthCookie");
                string soapResult = string.Empty;
                client.UploadString(new Uri(serviceURL), request);
                client.UploadStringCompleted += new UploadStringCompletedEventHandler(delegate(object sender, UploadStringCompletedEventArgs e)
                {
                    soapResult = e.Result;
                });                
                return soapResult;
            }
        }
        internal static string GetFedAuthCookieFromSOAPResponse(string soapResponse)
        {
            string openingTag = "<GetFedAuthCookieResult>";
            string closingTag = "</GetFedAuthCookieResult>";
            int startIndex = soapResponse.IndexOf(openingTag) + openingTag.Length;
            int length = soapResponse.IndexOf(closingTag) - startIndex;
            return soapResponse.Substring(startIndex, length);
        }
static void Main(字符串[]args)
{
字符串url=”https://server/site";
字符串soapResponse=GetSOAPResponse(url,“http://site/server/exapmle.asmx");
Console.WriteLine(GetFedAuthCookieFromSOAPResponse(soapResponse));
Console.WriteLine(“按ENTER键关闭程序”);
Console.ReadLine();
}
内部静态字符串GetSOAPResponse(字符串url、字符串服务url)
{
使用(WebClient=newWebClient())
{
字符串请求=“url+”;
client.Headers.Add(HttpRequestHeader.ContentType,“text/xml;charset=utf-8”);
client.Headers.Add(“SOAPAction”http://tempuri.org/GetFedAuthCookie");
string soapResult=string.Empty;
UploadString(新Uri(serviceURL),请求);
client.UploadStringCompleted+=新的UploadStringCompletedEventHandler(委托(对象发送方,UploadStringCompletedEventArgs e)
{
soapResult=e.结果;
});                
返回结果;
}
}
内部静态字符串GetFedAuthCookieFromSOAPResponse(字符串soapResponse)
{
字符串openingTag=“”;
字符串closingTag=“”;
int startIndex=soapResponse.IndexOf(openingTag)+openingTag.Length;
int length=soapResponse.IndexOf(closingTag)-startIndex;
返回soapResponse.Substring(startIndex,length);
}

这是您响应web服务请求完成的地方:

client.UploadStringCompleted += new UploadStringCompletedEventHandler(delegate(object sender, UploadStringCompletedEventArgs e)
{
    soapResult = e.Result;
});
因此,您可以在此处执行后续逻辑:

client.UploadStringCompleted += new UploadStringCompletedEventHandler(delegate(object sender, UploadStringCompletedEventArgs e)
{
    Console.WriteLine(GetFedAuthCookieFromSOAPResponse(e.Result));
    Console.WriteLine("Press ENTER to close program");
    Console.ReadLine();
});
或者您可以将该逻辑重构为一个方法,并从中调用该方法,等等


但本质上,由于这是一个异步操作,您不需要“等到它完成”,而是“响应它的完成事件”。

这是您响应web服务请求完成的地方:

client.UploadStringCompleted += new UploadStringCompletedEventHandler(delegate(object sender, UploadStringCompletedEventArgs e)
{
    soapResult = e.Result;
});
因此,您可以在此处执行后续逻辑:

client.UploadStringCompleted += new UploadStringCompletedEventHandler(delegate(object sender, UploadStringCompletedEventArgs e)
{
    Console.WriteLine(GetFedAuthCookieFromSOAPResponse(e.Result));
    Console.WriteLine("Press ENTER to close program");
    Console.ReadLine();
});
或者您可以将该逻辑重构为一个方法,并从中调用该方法,等等


但本质上,由于这是一个异步操作,您不需要“等到它完成”,而是“响应它的完成事件”。

UploadString
已经是一种同步方法。这意味着当方法返回时,操作已完成
UploadStringCompleted
仅在处理异步方法时使用,如
UploadStringAsync
UploadStringTaskAsync

UploadString
返回一个字符串,该字符串是服务器的响应

你只需要

string soapResult = client.UploadString(new Uri(serviceURL), request);
return soapResult;

UploadString
已经是一种同步方法。这意味着当方法返回时,操作已完成
UploadStringCompleted
仅在处理异步方法时使用,如
UploadStringAsync
UploadStringTaskAsync

UploadString
返回一个字符串,该字符串是服务器的响应

你只需要

string soapResult = client.UploadString(new Uri(serviceURL), request);
return soapResult;

我想等到电话打完。还有其他方法使用方法“GetFedAuthCookieFromSOAPResponse”@Karthik的返回值:任何使用异步操作返回值的方法都需要由该操作的响应处理程序调用。现在,如果web服务代理公开了一个同步方法调用,那就完全是另一回事了。@Karthik:进一步检查后,看起来调用实际上是同步的。关于这个问题的另一个答案可能会更简单。我想等到通话结束。还有其他方法使用方法“GetFedAuthCookieFromSOAPResponse”@Karthik的返回值:任何使用异步操作返回值的方法都需要由该操作的响应处理程序调用。现在,如果web服务代理公开了一个同步方法调用,那就完全是另一回事了。@Karthik:进一步检查后,看起来调用实际上是同步的。关于这个问题的另一个答案可能会更简单。