HTTPS with WebResponse class

From: Aung (aung_at_aungkyawmoe_NOSPAM_.net)
Date: 08/29/03


Date: Sat, 30 Aug 2003 04:19:52 +0700


Hello,

I am having a problem with WebRequest class. I used the following code to
post some data to a page and retrive back the response. It works fine with
http:// connection. When i use that with https:// connection, it thrown an
exception "The underlying connection was closed: Could not establish trust
relationship with remote server.". I am using self sign certificate on the
web server and the root and CA certificates are installed on the client
machine using the following code.

your suggestions will be great appreciated.

Aung

public string postForm(string url, string postData)

{

string retStr="", tempStr;

HttpWebResponse result = null;

try

{

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);

req.Method = "POST";

req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR
1.0.3705)";

req.ContentType = "application/x-www-form-urlencoded";

StringBuilder UrlEncoded = new StringBuilder();

Char[] reserved = {'?', '=', '&'};

byte[] SomeBytes = null;

if (postData != null)

{

int i=0, j;

while(i<postData.Length)

{

j=postData.IndexOfAny(reserved, i);

if (j==-1)

{

UrlEncoded.Append(HttpUtility.UrlEncode(postData.Substring(i,
postData.Length-i)));

break;

}

UrlEncoded.Append(HttpUtility.UrlEncode(postData.Substring(i, j-i)));

UrlEncoded.Append(postData.Substring(j,1));

i = j+1;

}

SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());

req.ContentLength = SomeBytes.Length;

Stream newStream = req.GetRequestStream();

newStream.Write(SomeBytes, 0, SomeBytes.Length);

newStream.Close();

}

else

{

req.ContentLength = 0;

}

result = (HttpWebResponse) req.GetResponse();

Stream ReceiveStream = result.GetResponseStream();

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader sr = new StreamReader( ReceiveStream, encode );

Char[] read = new Char[256];

int count = sr.Read( read, 0, 256 );

while (count > 0)

{

tempStr = new String(read, 0, count);

retStr += tempStr;

count = sr.Read(read, 0, 256);

}

retStr.Trim();

}

catch (Exception e)

{

retStr = "Error!!";

}

finally

{

if ( result != null )

{

result.Close();

}

}

return retStr;

}



Relevant Pages

  • Re: HTTPS with WebResponse class
    ... Best way to test your client is see if you can do the same from IE. ... > public string postForm ... > result = (HttpWebResponse) req.GetResponse; ... > Stream ReceiveStream = result.GetResponseStream; ...
    (microsoft.public.dotnet.security)
  • RE:.NET SAP Connector / single sign on via ASP.NET
    ... string username = "BLA"; ... HttpWebResponse response = null; ... request.Method = "POST"; ... Stream receive_stream = response.GetResponseStream; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Retrieve Form Key-Values from HttpWebResponse
    ... > which reads into a stream and then into a string. ... HttpWebResponse itself is completely agnostic to what it contains -- it ... you need to search through the HTML document. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: socket problem
    ... Note that your server has *already* accepted the connection at this ... String fname1=br.readLine; ... The issue is br.readlinereads the input stream only sometimes ...
    (comp.lang.java.help)
  • Re: Question about design, defmacro, macrolet, and &environment
    ... "Expects to find the literal string on the stream." ... (defun send (string &optional stream) ... (declaim (inline make-adjustable-string)) ...
    (comp.lang.lisp)