WebResponse error 401

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please help figure what I am doing wrong in this code. All I am trying to do is send come data to an aspx page and get some data back. I watched my variable in debug mode and can tell that I am reaching the web site but my data seems not to be posted. Also in debug mode I get error "Remote server returned error 401: Unautherized) at the "WebResponse Response = Request.GetResponse();" line and debugging stops. I have the required certificate required to access this site installed on my machine and can run a similar VB script and get resuilt back - but need to convert this to C#.

string sURL = "https://cabman/Purchase.aspx"
string cData = "<CABDETAILS CabNumber=""43877"" CabSKU=""6633"" ExpDate=""0106"" Postal=""78052"" />");
StringBuilder postData = new StringBuilder(cData);
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(sURL)
Request.ContentType="text/xml"
Request.ContentLength = postData.Length
Request.Method="POST"
//Request.KeepAlive = false
Stream newStream=Request.GetRequestStream()
StreamWriter sw = new StreamWriter(newStream)
sw.Write(postData.ToString())
sw.Close()

//Now we need to read the dat
WebResponse Response = Request.GetResponse()
HttpWebResponse httpRes = (HttpWebResponse)Response
Stream s = httpRes.GetResponseStream()
StreamReader sr = new StreamReader(s,Encoding.ASCII)
string returnDoc = sr.ReadToEnd()
sr.Close()

Thanks
Jacob
 
Jacob said:
Please help figure what I am doing wrong in this code. All I am
trying to do is send come data to an aspx page and get some data
back. I watched my variable in debug mode and can tell that I am
reaching the web site but my data seems not to be posted. Also in
debug mode I get error "Remote server returned error 401:
Unautherized) at the "WebResponse Response = Request.GetResponse();"
line and debugging stops. I have the required certificate required to
access this site installed on my machine and can run a similar VB
script and get resuilt back - but need to convert this to C#.
[...]

You don't set your client certificate in the code you've posted. Check out
System.Net.HttpWebRequest's ClientCertificates property.

Cheers,
 
Back
Top