3

I'm making a code that reads the a page and downloads it contents programmatically, but it does not work same way as a browser. Note that I'm also using cookies string.

my code is:

string strUrl = "http:" + "//mgac.webex." + "com";
string cookies_str = "vSeg=post_attendee; s_nr=1321305381566-New; s_lv=1321305381566; s_vnum=1322686800567%26vn%3D1; galaxyb_wl=R2355168776; JSESSIONID=Qlq1TR7Hf09KTsGHr4vv2GnTFF0NGRlLmGyYmMvzY5M29pbZ8yNp!31020270; DetectionBrowserStatus=3|1|32|1|4|2; CK_LanguageID_503319=1; CK_TimeZone_503319=4; CK_RegionID_503319=2; vSeg=post_attendee; s_nr=1321305381566-New; s_lv=1321305381566; s_vnum=1322686800567%26vn%3D1; galaxyb_wl=R2355168776; JSESSIONID=Qlq1TR7Hf09KTsGHr4vv2GnTFF0NGRlLmGyYmMvzY5M29pbZ8yNp!31020270;";
string other_saved_cookies = "screenWidth=1280; CK_CDNHostStatus=akamaicdn.webex.com|1322367753273|1";

string s;
using (WebClient client = new WebClient())
{
    client.UseDefaultCredentials = true;
    client.Headers.Add(HttpRequestHeader.Cookie, cookies_str);
    s = client.DownloadString(strUrl);
}

I get this answer: "The Page Cannot be found..."

when I scan the Request with Fiddler, my browser recieves the same answer, after that he makes a new request to using SSL to same host.

How can I make exactly same request to receive content like a browser

Where is the information that tells client: "a SSL connection is required" ?

Alexei
  • 1,289
  • 3
  • 19
  • 34

4 Answers4

6

This looks like what you're after How to use HTTP GET request in C# with SSL? (protocol violation)

But changing "http" to "https" might be a good starting point!
Then, apparently, setting a ServicePointManager.ServerCertificateValidationCallback as shown in 2 of the replies to the above post.

EDIT

Add

string ua = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";

then

client.Headers.Add(HttpRequestHeader.UserAgent, ua);

Then parse the Location header of the redirected result.

Community
  • 1
  • 1
shunty
  • 3,699
  • 1
  • 22
  • 27
  • how the client must know that a secure connection is requiered? I can't get it in the Response – Alexei Nov 28 '11 at 04:36
  • Strictly speaking I don't think you can infer the need for SSL from the response. See this SO thread: http://stackoverflow.com/questions/2554778/what-is-the-proper-http-response-to-send-for-requests-that-require-ssl – shunty Nov 28 '11 at 08:52
  • I don't want to infer, I only want to make code, that makes a https connection if that is needed, understanting it from the first response made to http, exactly same way that the code shows, you could try with same url that is specified in example code. – Alexei Nov 30 '11 at 07:09
  • Take a more detailed look with Fiddler when you make the request from a browser. The response is different if you add a User-Agent string. Without that header you get a redirect (302) followed by a success (200). With that header you get a redirect, then another redirect with a Location header pointing you to the https site. So try, for example: string ua = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0"; client.Headers.Add(HttpRequestHeader.UserAgent, ua); Then parse the headers. – shunty Nov 30 '11 at 08:11
2

This web site uses 302 redirects to bring you to the SSL page.

The WebClient .NET class is a smart class that automatically follows the redirects. It uses the HttpWebRequest class and sets the AllowAutoRedirect to true.

So if you issue your request to the original URL, the WebClient steps over the redirects (using new requests) while the result is 302 (some kind of redirect). If the result code is different, you will get the result. It looks like a single http request, but it does 2 http and 1 https calls.

You have to set the user agent in this case as shunty posted it previously, because the remote site not likes the .NET's default user agent: none.

Marton Rusko
  • 370
  • 2
  • 8
1

Check this msdn link HTTP Security and ASP.NET Web Services

By default, the Require secure channel (SSL) check box is clear; select it to require SSL. SSL supports both 40-bit and 128-bit encryption. The more bits used by the encryption, the harder it is to break it and figure out what the original bits were.

Once you have the resource set up to require SSL for communications, any messages sent between the sender and receiver will be encrypted and signed. This means that outside parties cannot read the contents of the messages. If an outside party changes the bytes in the message, the message receiver can detect it.

You Should need some Credential or Certificate information of run your code perfectly.

You could use the RemoteCertificateValidationCallback delegate for validating the SSL certificate

Check these links for code help:

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

How to use HTTP GET request in C# with SSL? (protocol violation)

How do I use WebRequest to access an SSL encrypted site using https?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • At the beggining browser makes a http (not secure) connection, and it receives "Page not found", but where is the information that tells to the browser that he must make a secure connection? – Alexei Nov 28 '11 at 04:36
0

If you simply want to load up a page via https (ssl), this will work. If you want to take a look at the certificate, I recommend using a full delgate method

ServicePointManager.ServerCertificateValidationCallback = ( ( sender , certificate , chain , sslPolicyErrors ) => true );         //allows for validation of SSL certificates 

Or with full delegate method

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback( delegateHttpSsl );

        private bool delegateHttpSsl(object obj, System.Security.Cryptography.X509Certificate c1, System.Security.Cryptography.X509Certifciates.X509Chain c2, SslPolicyErrors c3)
        {
              return true;
        }
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44