Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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);
            }
        }
    }
}


Thanks
Manoj
Posted
Updated 11-Aug-13 6:05am
v2
Comments
virusstorm 13-Aug-13 16:08pm    
It looks like you are doing things out of order. Check this out:
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

If you are still have issues, let me know I can try and create simple example later this week.

1 solution

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()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900