Tuesday, September 15, 2009

Form POST using HttpWebRequest but on SSL

In my last post i presented the code snippet for posting form data using HttpWebRequest with POST method.. It was working fine until the page I was accessing, transferred to SSL and my application ended up with "The underlying connection was closed: Could not establish trust relationship with remote server." exception.  Without going through MSDN i logged in to forums.asp.net and guess what? yeah found the solution...
Just create a Certificate Policy which accept all Certificates...bus!

public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
public TrustAllCertificatePolicy()
{ }

public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert, WebRequest req, int problem)
return true;

As you can see CheckValidationResult always return true thus accepting all certificates...

To use this CertificatePolicy, you'll have to tell the ServicePointManager to use it:
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

Thanks to:

using HttpWebRequest and HttpWebResponse classes

Sending form values with GET method is really easy just appened the URL with ? (question mark) followed with name,value pair but how to send values to the form with POST method? hmm this is what i was searching for few days ago and then i found HttpWebRequest and HttpWebResponse classes. It's my introduction with these classes. (Now after more than 2 years, I have realised the importance of these two classes)

Microsoft made it really easy - just use httpWebRequest to make request and httpWebResponse to get response Voila! thats it!
Have a look at the code below:

string GetResponseString(string Url, string postString)
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Url);
httpRequest.Method = "POST";
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
httpRequest.CookieContainer = new CookieContainer();
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(postString);
httpRequest.ContentLength = bytedata.Length;
httpRequest.CookieContainer = ccContainer;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
string line;
while ((line = reader.ReadLine()) != null)
return sb.ToString();

Url is the location of page where form exists and poststring is basically of form name:value pair.
For example our page is located @
http://mytechblog.com/formResultpage.aspx, hosting a form with two input fields, named Field1 and Field2. In this case the the postString will be:

string postString = String.Format("field1={0}&field2={1}", "Field 1 Value", "Field 2 Value");

GetResponseString() will return the response stream containing the page generated after form processing..

string szResult = GetResponseString("http://mytechblog.com/formResultpage.aspx",postString);

Extracting anchor tag <a> using C# RegEx

I was in need of a parsing and extracting all the anchor tags within an HTML file. First I tried using some string manipulation technique but that was a mess!! Then i tried to use regular expression to achive the same, but it since I am not good at regular expressions (not even bad :) ), it gave me some really hard time. But like always web was there so save me, and by combining my search and programming expertise atlast I was able to write a piece of code that can extract all anchor "<a>" tags with css class from an html file...

The code is give below, which first reads a webpage and save it's HTML in a string variable.

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://enggwaqas.spaces.live.com");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream());
                string szResult = sr.ReadToEnd();
               string pattern= @"<a.*?href=[""'](?<url>.*?)[""'] ?(class=[""']linkClass[""']).*?>(?<name>.*?)</a>";
               MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);

               foreach(Match m in matches)
             catch(Exception e){}

It will not extract all the anchor tags but those with cssClass set to 'linkClass', why? Because I write the code this way :)