Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to access the SAP WSDL service in c#.net. Even though I have put correct credentials it throws "The remote server returned an error: (401) Unauthorized." error. I am able to access the SAP service using SoapUI application with these same credentials on the same machine. I need to connect to VPN before accessing these SAP services. Not sure what is causing this problem.
Is there anything specific that needs to be done here?

What I have tried:

I have tried the following codes.
1.
ASP.NET
System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = new NetworkCredential("", "");
client.Headers[HttpRequestHeader.Authorization] = "Basic";
//-Connect To the web service Getting 401 error in following line
using (System.IO.Stream stream = client.OpenRead("***?sap-client=200"))
{
}

2.
ASP.NET
public void InvokeService()
        {

            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"*****?sap-client=200");
            Req.Credentials = new NetworkCredential("***", "***", "***");
            Req.Headers.Add(@"SOAP:Action");
            Req.ContentType = "text/xml;charset=\"utf-8\"";
            Req.Accept = "text/xml";
            Req.Method = "POST";

            XmlDocument SOAPReqBody = new XmlDocument();
            SOAPReqBody.LoadXml(@"<?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>
                <Addition xmlns=""http://tempuri.org/"">
                  <a>" + 10 + @"</a>
                  " + 20 + @"
                </Addition>
              </soap:Body>
            </soap:Envelope>");
            using (Stream stream = Req.GetRequestStream())
            {
                SOAPReqBody.Save(stream);
            }
            //Getting 401 error in following line
            using (WebResponse Serviceres = Req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
                {

                }
            }
        }
Posted
Comments
NotTodayYo 12-Mar-21 16:19pm    
If you are sure the credentials are correct then you're probably missing a header. You'll have to check with whoever owns the webservice.

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