tag:blogger.com,1999:blog-2682074227462239322024-02-20T10:10:33.791-08:00Vicki's Tech BlogVicki'shttp://www.blogger.com/profile/16387649824922018891noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-268207422746223932.post-9522093728521158712009-09-15T03:18:00.000-07:002009-09-15T03:18:57.250-07:00Form POST using HttpWebRequest but on SSLIn 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...<br />
Just create a Certificate Policy which accept all Certificates...bus!<br />
<br />
<span style="color: blue;">public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy</span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">public TrustAllCertificatePolicy()</span><br />
<span style="color: blue;">{ }</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">public bool CheckValidationResult(ServicePoint sp,</span><br />
<span style="color: blue;">X509Certificate cert, WebRequest req, int problem)</span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">return true;</span><br />
<span style="color: blue;">}</span><br />
<span style="color: blue;">}</span><br />
<br />
As you can see CheckValidationResult always return true thus accepting all certificates...<br />
<br />
To use this CertificatePolicy, you'll have to tell the ServicePointManager to use it:<br />
<span style="color: blue;">System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();</span><br />
...............................................<br />
<br />
Thanks to:<br />
http://weblogs.asp.net/jan/archive/2003/12/04/41154.aspxVicki'shttp://www.blogger.com/profile/16387649824922018891noreply@blogger.com0tag:blogger.com,1999:blog-268207422746223932.post-60543023682397817212009-09-15T03:02:00.001-07:002009-09-15T03:02:57.525-07:00using HttpWebRequest and HttpWebResponse classes<div>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)<br />
<br />
Microsoft made it really easy - just use httpWebRequest to make request and httpWebResponse to get response Voila! thats it!</div><div>Have a look at the code below:<br/><br />
</div><div><span style="color: blue;"></span><span style="color: black;"></span><span style="color: blue;">string</span><span style="color: black;"> GetResponseString(</span><span style="color: blue;">string</span><span style="color: black;"> Url, </span><span style="color: blue;">string</span><span style="color: black;"> postString)<br />
</span>{<br />
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Url);<br />
httpRequest.Method = "POST";<br />
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";<br />
httpRequest.CookieContainer = <span style="color: blue;">new</span> CookieContainer();<br />
httpRequest.ContentType = "application/x-www-form-urlencoded";<br />
<span style="color: blue;">byte</span>[] bytedata = Encoding.UTF8.GetBytes(postString);<br />
httpRequest.ContentLength = bytedata.Length;<br />
httpRequest.CookieContainer = ccContainer;<br />
Stream requestStream = httpRequest.GetRequestStream();<br />
requestStream.Write(bytedata, 0, bytedata.Length);<br />
requestStream.Close();<br />
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();<br />
Stream responseStream = httpWebResponse.GetResponseStream();<br />
ccContainer.Add(httpWebResponse.Cookies); <br />
StringBuilder sb = <span style="color: blue;">new</span> StringBuilder();<br />
<span style="color: blue;">using</span> (StreamReader reader = <span style="color: blue;">new</span> StreamReader(responseStream, System.Text.Encoding.UTF8))<br />
{<br />
<span style="color: blue;">string</span> line;<br />
<span style="color: blue;">while</span> ((line = reader.ReadLine()) != <span style="color: blue;">null</span>)<br />
{<br />
sb.Append(line);<br />
}<br />
}<br />
<span style="color: blue;">return</span> sb.ToString();<br />
} <br />
<br />
<br />
<br />
<span style="color: black;">Url is the location of page where form exists and poststring is basically of form name:value pair.<br />
For example our page is located @ </span><a href="http://mytechblog.com/formResultpage.aspx"><span style="color: black;">http://mytechblog.com/formResultpage.aspx</span></a>, <span style="color: black;">hosting a form with </span><span style="color: black;">two input fields, named Field1 and Field2. In this case the the postString will be:<br />
</span><br />
<span style="color: blue;">string postString = String.Format("field1={0}&field2={1}", "Field 1 Value", "Field 2 Value");</span><br />
<span style="color: blue;"></span><br />
<span style="color: black;"><strong><em>GetResponseString()</em></strong> will return the response stream containing the page generated after form processing..</span><br />
<span style="color: black;"><br />
<br />
string szResult = GetResponseString("http://mytechblog.com/formResultpage.aspx",postString);</span></div>Vicki'shttp://www.blogger.com/profile/16387649824922018891noreply@blogger.com0tag:blogger.com,1999:blog-268207422746223932.post-66652256109503505562009-09-15T02:48:00.001-07:002009-09-15T02:48:14.755-07:00Extracting anchor tag <a> using C# RegEx<div><span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;">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... </span><br />
<br />
<span style="font-family: Arial;">The code is give below, which first reads a webpage and save it's HTML in a string variable.</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"><br />
</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> HttpWebRequest request = (HttpWebRequest)WebRequest.Create("</span><a href="http://enggwaqas.spaces.live.com/"><span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;">http://enggwaqas.spaces.live.com</span></a><span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;">");</span></div><div><span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> try</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> {</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> HttpWebResponse response = (HttpWebResponse)request.GetResponse();</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> StreamReader sr = new StreamReader(response.GetResponseStream());</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> string szResult = sr.ReadToEnd();</span><br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> sr.Close();</span></div><span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;"> string pattern= @"<a.*?href=[""'](?<url>.*?)[""'] ?(class=[""']linkClass[""']).*?>(?<name>.*?)</a>";<br />
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);<br />
<br />
foreach(Match m in matches)<br />
Console.WriteLine(m.Value);<br />
}<br />
catch(Exception e){} </span><br />
<br />
<span style="font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;">It will not extract all the anchor tags but those with cssClass set to 'linkClass', why? Because I write the code this way :)</span>Vicki'shttp://www.blogger.com/profile/16387649824922018891noreply@blogger.com0