Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/**************************************WebClient Attempt*********************/
public static void CallWithWebClient()
    {
        Uri baseUri = new Uri(@"https://qapartsb2bwsx.rd.cat.com/psb2b/ReceiveDocumentServlet/xCBL30");
        string username = "A1rypsipc";
        string password = "catparts#1";
        var client = new System.Net.WebClient { Credentials = new NetworkCredential("username", "password") };

        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.Load("Caterpillar.xml");
        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        string text = soapEnvelopeXml.InnerXml;
        string response =
            client.UploadString(baseUri, text);


    }


/***********************HttpWebRequestAttempt******************************/
public static void CallWithWebrequest()
   {
       Uri baseUri = new Uri(@"https://qapartsb2bwsx.rd.cat.com/psb2b/ReceiveDocumentServlet/xCBL30");
       string username = "A1rypsipc";
       string password = "catparts#1";
       WebRequest rqst = WebRequest.Create(baseUri);
       string authInfo = username + ":" + password;
       authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
       rqst.Headers.Add("Authorization", "Basic " +  authInfo);
       rqst.ContentType = "application/xhtml+xml";
       NetworkCredential cred = new NetworkCredential(username, password);
       rqst.Credentials = cred;
       rqst.PreAuthenticate = true;
       rqst.Method = "POST";
       XmlDocument soapEnvelopeXml = new XmlDocument();
       XmlTextReader rdr = new XmlTextReader("Caterpillar.xml");
       soapEnvelopeXml.Load("Caterpillar.xml");

       byte[] byteArray = Encoding.UTF8.GetBytes(soapEnvelopeXml.InnerXml);

       rqst.ContentLength = byteArray.Length;
       Stream dataStream = rqst.GetRequestStream();
       dataStream.Write(byteArray, 0, byteArray.Length);
       dataStream.Close();
       WebResponse response = rqst.GetResponse();
       dataStream = response.GetResponseStream();
       StreamReader reader = new StreamReader(dataStream);



       Stream data = rqst.GetRequestStream();
       soapEnvelopeXml.Save(data);
       dataStream.Close();
       using (WebResponse res = rqst.GetResponse())
       {
           using (StreamReader rd = new StreamReader(res.GetResponseStream()))
           {
               string soapResult = rd.ReadToEnd();
               Console.WriteLine(soapResult);
           }
       }


What I have tried:

I have tried many many different suggestions on forums everywhere, with no success. Most are pretty much identical. The httpwebrequest one uses the networkCredential to provide userid and pwd, then I can send the xml through the request stream, but it fails authentication every time. It thinks the userid and pwd are null. The WebClient actually passes authentication (I assume because it gets past that part long enough to tell me it has no XML document attached).

If anyone can help me figure out what I am doing wrong, I would be very much appreciative.
Posted

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