通过HTTP调用Soap Web服务,提供异常“Connection close” [英] Calling Soap web service over HTTP giving exception "Connection close"

查看:108
本文介绍了通过HTTP调用Soap Web服务,提供异常“Connection close”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过HTTP调用Web服务,但无法连接。例外是连接关闭。想知道为什么它被关闭虽然相同的SOAP消息在SOAP UI中工作。我可以在从SOAP UI复制时使用URN操作。请说明有什么问题。



我的C#代码如下。



I am calling Web service over HTTP but it is failing to connect. Exception is connection close. Wondering why it is closed though same SOAP message works in SOAP UI. Can I give Action with URN as I copyed from SOAP UI. Please suggest what is wrong.

My C# code is as follows.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace CTWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CallWebService();
        }
        public static void CallWebService()
        {
            var _url = "https://myService.com/webservices/ct/services/4.1";
            var _action = "urn:provider/interface/ctservices/getCPNInstances";

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // Start the asynchronous operation to get the response
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }

        private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation /* I'm getting the exception Connection Close."*/
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();
            Console.WriteLine(responseString);
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();           
        }

        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";
            webRequest.KeepAlive = false;
            webRequest.Timeout = 300000;
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns1=""urn:dictionary:com.ct.webservices""><SOAP-ENV:Header xmlns:wsse=""http://docs.myses.org/wss/2004/01/200401-wss-wssecurity-secext-1.0.xsd""><wsse:Security SOAP-ENV:mustUnderstand=""1""><wsse:UsernameToken><wsse:Username>fiwjiueji</wsse:Username><wsse:Password Type=""http://docs.myses.org/wss/2004/01/200401-wss-username-token-profile-1.0#PasswordText"">tjrrfrsi</wsse:Password></wsse:UsernameToken></wsse:Security></SOAP-ENV:Header><SOAP-ENV:Body><ns1:getCPNInstances></ns1:getCPNInstances></SOAP-ENV:Body></SOAP-ENV:Envelope>");
            return soapEnvelop;
        }

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





谢谢

Manoj



Thanks
Manoj

推荐答案

订单是正确的。我的python代码连接但又在C#中怎么做。看看它是否有助于你修复。



进口suds

来自suds.client import客户

来自suds.wsse import *

def main():



url ='https://webservices.cp.com/webservices/ cphargepoint / services / 4.1'

wsdl ='https://webservices.cp.com/api.wsdl'

#API用户和密码

api_user ='yrewte44'

api_pass ='eg430'



#创建客户端并在soap标头中添加安全令牌

client =客户端(url = wsdl,location = url)

security =安全()

token = UsernameToken(api_user,api_pass)

security.tokens.append(令牌)

client.set_options(wsse =安全)

试试:

#un-comment下面的print语句查看所有已发布的

#CP服务SOAP方法的列表。

#print client



#getPublicStations()服务方法接受一种'stationSearchRequest'

searchQuery = client.factory.create('stationSearchRequest')

#add properties / filter options

searchQuery.Proximity = 10

searchQuery.proximityUnit ='M'

#create goeData,提供起点坐标

geoData = client.factory.create('geoData')

geoData.Lat = 37.425758

geoData.Long = -122.097807

searchQuery .Geo = geoData



#这里是对服务的实际调用

response = client.service.getPublicStations(searchQuery)

#对数据做任何事情

#打印回复

除了suds.WebFault详细信息:

打印细节


if __name __ ==__ main__:

main()
Order is correct. I python code to connect but again how to do in C#. See if it can help in fixing for you.

import suds
from suds.client import Client
from suds.wsse import *
def main():

url = 'https://webservices.cp.com/webservices/cphargepoint/services/4.1'
wsdl = 'https://webservices.cp.com/api.wsdl'
# API user and password
api_user = 'yrewte44'
api_pass = 'eg430'

# create client and add security tokens in the soap header
client = Client(url=wsdl, location=url)
security = Security()
token = UsernameToken(api_user, api_pass)
security.tokens.append(token)
client.set_options(wsse=security)
try:
# un-comment the print statement below to see the list of all published
# CP service SOAP methods.
# print client

# getPublicStations() service method accepts a type of 'stationSearchRequest'
searchQuery = client.factory.create('stationSearchRequest')
# add properties/filter options
searchQuery.Proximity = 10
searchQuery.proximityUnit = 'M'
# create goeData, provide starting point co-ordinates
geoData = client.factory.create('geoData')
geoData.Lat = 37.425758
geoData.Long = -122.097807
searchQuery.Geo = geoData

# here is the actual call to the service
response = client.service.getPublicStations(searchQuery)
# do whatever with the data
# print response
except suds.WebFault as detail:
print detail

if __name__=="__main__":
main()


这篇关于通过HTTP调用Soap Web服务,提供异常“Connection close”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆