/**************************************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.