HttpWebRequest with https

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
Hi to all,

im trying to send logon information via GET to a website., but i'm having some problems. I'm using 1.1 with vs2003. This is the code i have right now

C#:
string sUrl;
HttpWebRequest oRequest;
CookieContainer oCookieContainer;
HttpWebResponse oResponse;
Stream oStream;
StreamReader oStreamReader;
string responseFromServer;

sUrl = "https://hotspot.ptwifi.pt/user?lg=pt&username=XXXXXXX&password=YYYYYYY";
//sUrl = "http://www.google.com";
oRequest = (HttpWebRequest)(WebRequest.Create(sUrl));
oRequest.Method = "GET";
oCookieContainer = new CookieContainer();
oRequest.CookieContainer = oCookieContainer; //this allows the cookies sent '\b' to be loaded;

oResponse = (HttpWebResponse)(oRequest.GetResponse());
oStream = oResponse.GetResponseStream();
oStreamReader = new StreamReader(oStream);
responseFromServer = oStreamReader.ReadToEnd();
Console.WriteLine (responseFromServer);//i use the result here
Console.ReadLine();



The error message i get is this

Code:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship with remote server.
   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleApplication1.Class1.Main(String[] args) in c:\documents and settings\rfazendeiro\my documents\visual studio projects\consoleapplication1\class1.cs:line 35


does anyone have any ideia how to resolve this?
thx in advance
 
You are most likely experiencing the same problem as I described here:

http://www.xtremedotnettalk.com/showthread.php?t=96720&highlight=Certificate

You have problably encountered this error at some point while browsing the web (see attached image). When it happens in a web browser your browser will notify you with a popup and ask you what to do. When communicating with SSL programmatically, .NET will blow up and throw the Exception you received unless you specify how you want to handle the situation ahead of time.

You need to create a class that implements the ICertificatePolicy interface (creating a policy regarding how you want to handle trust related issues) and then bind it to your request.
 

Attachments

Back
Top