JD11
JD11

Reputation: 314

Authentication with SoapHttpClientProtocol

I need to request webservices through forefront endpoint. I use basic authentication. it work perfectly if I use this code :

Uri uri = new Uri("https://ged.legrandnarbonne.com/_vti_bin/webs.asmx");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

        System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();
        credentialCache.Add(
            new System.Uri("https://xxx.com"),
            "Basic",
            new System.Net.NetworkCredential("xxx", "xxxx")
        );
        string postData = @"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetWeb xmlns='http://schemas.microsoft.com/sharepoint/soap/'><webUrl>https://ged.legrandnarbonne.com</webUrl></GetWeb></soap:Body></soap:Envelope>";
        byte[] byteArray = Encoding.UTF8.GetBytes (postData);
        request.ContentType = "text/xml; charset=utf-8";

        request.Credentials=credentialCache;
        request.Method = "POST";
        request.ContentLength=byteArray.Length;
        Stream dataStream = request.GetRequestStream ();
        dataStream.Write (byteArray, 0, byteArray.Length);
        dataStream.Close ();

        WebResponse v = request.GetResponse();
        Stream rStream = v.GetResponseStream();
        StreamReader str = new StreamReader(rStream);
        if (str.EndOfStream != true)
        {
            Console.WriteLine(str.ReadToEnd());
        }
        v.Close();
        rStream.Close();
        str.Close();

I got a 401 response and then authentication is send and I got the response.

If I use SoapHttpClientProtocol, I have a 302 response and I don't now how to handle this. Could you help me.

Upvotes: 1

Views: 4695

Answers (1)

JD11
JD11

Reputation: 314

After a long investigation it look like redirection is due to "useragent" header. if it is set TMG send a 302 to redirect request to the login page. If it's left blank it work perfectly. Hope this can help someone.

Upvotes: 1

Related Questions