Skip to content

HttpWebRequest to https server works in fiddler but NOT from Visual Studio

Chad

Asked Apr 15, 2011, 5:59 AM EDT4 answers
Share
LinkedInX

Here is the scenario. I have written code use a digital certificate to GET cookies from a secured url, to in turn POST data back to another url using the retrieved cookies and same digital certificate. The GET works and cookies are retrieved, the POST comes back with an error 500. I installed fiddler to see what was going on...POST looks fine...cookies are present. I used the feature in fiddler that allows creating a request via drag and drop. POST the exact same POST recorded from C# code that was recorded in fiddler and it works!

What is Fiddler doing that Visual Studio is not? It must be doing something if fiddler can POST the data but Visual Studio returns an error 500. Here is the code below:

        X509Certificate cert = new X509Certificate("mycert.pfx", "certpassword"); 
       
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://servertoGETcookies/fileUpload.html";); 
        req
.CookieContainer = new CookieContainer(); 
        req
.Method = "GET"; 
        req
.ClientCertificates.Add(cert); 
 
       
HttpWebResponse Response = (HttpWebResponse)req.GetResponse(); 
       
CookieCollection ck = req.CookieContainer.GetCookies(req.RequestUri); 
       
string strcookie = ck[0].Value; 
       
string strcookie2 = ck[1].Value; 
       
Response.Close(); 
 
        req
= (HttpWebRequest)WebRequest.Create("https://servertoPOSTdatawithcookies/main";); 
        req
.CookieContainer = new CookieContainer(); 
       
Cookie auth = new Cookie("_wl_authcookie_", strcookie2); 
       
Cookie jsess = new Cookie("JSESSIONID", strcookie); 
        auth
.Domain = "server"; 
        jsess
.Domain = "server"; 
        req
.CookieContainer.Add(auth); 
        req
.CookieContainer.Add(jsess); 
        req
.ClientCertificates.Add(cert); 
        req
.Method = "POST"; 
 
       
Byte[] data = ReadByteArrayFromFile("filewithdatatoPOST.txt"); 
        req
.ContentLength = data.Length; 
       
Stream myStream = req.GetRequestStream(); 
        myStream
.Write(data, 0, data.Length); 
        myStream
.Close(); 
 
       
HttpWebResponse Response2 = (HttpWebResponse)req.GetResponse(); 
       
Stream strm = Response2.GetResponseStream(); 
       
StreamReader sr2 = new StreamReader(strm); 
       
Response2.Close(); 

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
     Error code 500 is an server error. 

     Check this thread, using try-catch you can find the actual error.

     
    0
  • 0
    No. Still trying to figure it out.
    0
  • 0
    Solved!!!

    So after weeks of troubleshooting....6 different programmers....I figured it out.  I'm not sure if this is always true, but in this scenario the problem was that the url we were getting cookies from:
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(https://servertoGETcookies/fileUpload.html);

    was not the same as the url that we were posting the data back to:
    req
    = (HttpWebRequest)WebRequest.Create("https://servertoPOSTdatawithcookies/main";);

    Getting the cookies and posting back to the same url fixed the issue:
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(https://servertoGETandPOSTcookies/main");
    req = (HttpWebRequest)WebRequest.Create("https://servertoGETandPOSTcookies/main");
    0
  • 1 short reply
    • Suthish Nair
      0
      resolved?
      0