Web Service redirection with custom SOAP Header

Last post 07-07-2009 1:49 PM by johnwsaunders3. 6 replies.

Sort Posts:

  • Big Smile [:D] Web Service redirection with custom SOAP Header

    07-03-2009, 6:04 AM
    • Member
      6 point Member
    • Ramaraj.R
    • Member since 07-03-2009, 7:09 AM
    • Bangalore, India
    • Posts 7

    Currently we are using a client tool which will invoke a web service to do some activity. Since we have site minder authenticated web service we have added the custom soap header, so that by using the soap header site minder will authenticate the request. Till this while it was working fine. Now we need to move this web service to some other server / domain. We don’t have any control over the Client tool now. So is there any way to redirect the web service request for the old URL to the New web service URL with the custom soap header.

     

    Header looks something like this

     

    <soapenv:Header>

                <log:credentials>

                            <logonid>XXXX</logonid>

                            <password>xxxx</password>

                </log:credentials>

      </soapenv:Header>

     

    Thanks,

    RamCry

  • Re: Web Service redirection with custom SOAP Header

    07-04-2009, 6:07 AM
    • Contributor
      2,386 point Contributor
    • maverickhyd
    • Member since 03-25-2009, 6:38 AM
    • Posts 402

     Hi,

    see this code

    string webServiceUrl="MyURL";

    WebRequest Request = WebRequest.Create(webServiceUrl);

    HttpWebRequest HttpRequest = (HttpWebRequest)Request;

    HttpRequest.Method = "POST";

    HttpRequest.ContentType = "text/xml";

    HttpRequest.Headers.Add("SOAPAction: http://www.tempuri.org/" + WebServiceMethodName);

    Stream Stream = HttpRequest.GetRequestStream();

    StreamWriter SWriter = new StreamWriter(Stream);

    string SoapRequest = @"<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>";

    SoapRequest = SoapRequest + soapmessage;

    SoapRequest = SoapRequest + @"</soap:Body></soap:Envelope>";

    SWriter.Write(SoapRequest);

    SWriter.Close();

    WebResponse Response = null;

    StreamReader SReader = null;

    Response = HttpRequest.GetResponse();

    Stream ResponseStream = Response.GetResponseStream();

    SReader = new StreamReader(ResponseStream);

    string s = SReader.ReadToEnd();

    You can do by using webrequest and webresponse class

    u have to use system.Net namespace

    give your webservice url and add custom soap headers using httpwebrequest.headers.add(..

    Thats it.

    Please Mark as Answer if it helped You!
  • Re: Web Service redirection with custom SOAP Header

    07-05-2009, 7:19 PM
    Answer

    I don't know whether your code will help him, but it has a couple of serious flaws.


    First of all, you haven't implemented any using statements. Your code may leak resources when an exception is thrown. These should be used whenever you create an instance of a class that implements IDisposable.

    Second, you should never construct XML by contatenating strings. XML does not follow the same rules as strings. In particular, what happens if your message contains special characters, like "<"?

    Try the following. I haven't had time to test it thoroughly, but it at least compiles

    public XElement MakeSoapRequest(
        string webServiceUrl,
        string webServiceMethodName,
        XElement messageBody)
    {
        var request = WebRequest.Create(webServiceUrl);
        request.Method = "POST";
        request.ContentType = "text/xml";
        request.Headers.Add(
            "SOAPAction: http://www.tempuri.org/" + webServiceMethodName);
    
        using (var stream = request.GetRequestStream())
        {
            XNamespace soapNamespace =
                "http://schemas.xmlsoap.org/soap/envelope/";
            var envelope = new XElement(
                soapNamespace + "Envelope",
                new XAttribute(
                    XNamespace.Xmlns + "xsi",
                    "http://www.w3.org/2001/XMLSchema-instance"),
                new XAttribute(
                    XNamespace.Xmlns + "xsd",
                    "http://www.w3.org/2001/XMLSchema"),
                new XAttribute(
                    XNamespace.Xmlns + "soap", soapNamespace.NamespaceName),
                new XElement(soapNamespace + "Body", messageBody));
            using (var writer = XmlWriter.Create(stream))
            {
                envelope.WriteTo(writer);
            }
        }
    
        using (var response = request.GetResponse())
        {
            using (var responseStream = response.GetResponseStream())
            {
                using (var xmlReader = XmlReader.Create(responseStream))
                {
                    return XElement.Load(xmlReader);
                }
            }
        }
    }
    

    :

    John Saunders
  • Re: Web Service redirection with custom SOAP Header

    07-05-2009, 11:58 PM
    • Member
      6 point Member
    • Ramaraj.R
    • Member since 07-03-2009, 7:09 AM
    • Bangalore, India
    • Posts 7

    Thanks for your reply john.

    I need to know is there any way to redirect the web service request for the old URL to the New web service URL with the custom soap header. 

    We had a client appliation deployed in all the client machines & its a windows application.

    Now we have set the permanent redirection to that web service to another web service URL which is in another server.

    We can't go back to the client and ask them to re deploy the client application. So need a way to do this without touching the client application.

    Please suggest a way for it.

    Thanks & Regards,

    Ramaraj R

  • Re: Web Service redirection with custom SOAP Header

    07-06-2009, 3:01 AM

    First of all, for the future, can you tell me why you thought adding a SOAP header would have anything at all to do with redirection? A SOAP Header is data in the message. Some piece of code has to read it in order for it to be of use.

    Also, have you tried the client with the permanent redirection? Does it not work? In what way does it not work?

    John Saunders
  • Re: Web Service redirection with custom SOAP Header

    07-07-2009, 12:00 AM
    • Member
      6 point Member
    • Ramaraj.R
    • Member since 07-03-2009, 7:09 AM
    • Bangalore, India
    • Posts 7

    I agree with you John.

    We use siteminder authentication, if we pass the Security tocken in the SOAP header then the siteminder will authentiucate the request.

    Now if we set the permanent redirection in IIS then when I try to consume my web service it throws Document Moved exception, One of the web service will be set as  Ananymous access and from ther we are redirecting to a web service which is set for Siteminder authentication. Now the question is In this senario if I redirect, will siteminder authenticate the request?.

    When I tried with the permanent redirection it throws the following error

     The request failed with the error message:

    --

    <head><title>Document Moved</title></head>
    <body><h1>Object Moved</h1>This document may be found <a HREF="herehttp://localhost/.../test.asmx">here</a></body>

     

    --Ram

  • Re: Web Service redirection with custom SOAP Header

    07-07-2009, 1:49 PM

    This means that you're going to have to change the client. The client is not set up to permit automatic redirection. See the AllowAutoRedirect property.

    John Saunders
Page 1 of 1 (7 items)