POSTing to a webpage and using it’s response (HttpWebRequest and HttpWebResponse)

You might encounter a situation where you need to Post to a web page and read it’s
response.

Here’s a function that uses System.Net.HttpWebRequest and System.Net.HttpWebResponse
to do just that.

      ' Usage:
      '   Dim xmlDoc As Xml.XmlDocument
      '   xmlDoc.Load(MakeHttpRequest( _
                           "request=listPersons&filter=last(a*)", _
                           "/requestManager.php", _
                           "application/x-www-form-urlencoded")) 

      Protected Function MakeHttpRequest(
                                   ByVal data As String, _
                                   ByVal url As String, _
                                   ByVal contentType As String) _
                                   As System.IO.Stream
        Dim retVal As System.IO.Stream 
        Dim httpRequest As System.Net.HttpWebRequest 
        Dim httpResponse As System.Net.HttpWebResponse 
        Dim encoding As New System.Text.UTF8Encoding
        Dim uri As New System.Uri(url) 
        Dim postBytes As Byte() postBytes = encoding.GetBytes(data)      
        httpRequest = CType(System.Net.HttpWebRequest.Create(uri), _
                                     System.Net.HttpWebRequest)      
        httpRequest.ContentLength = postBytes.Length 
        httpRequest.Method = "POST" 
        httpRequest.ContentType      = contentType 
        Dim postStream As System.IO.Stream = _
                                 httpRequest.GetRequestStream()      
        postStream.Write(postBytes, 0, postBytes.Length) 
        postStream.Close() 
        httpResponse = CType(httpRequest.GetResponse(), System.Net.HttpWebResponse) 
        retVal = httpResponse.GetResponseStream()
        Return retVal
      End Function
      

Variation: If the webpage you are calling expects xml you could easily replace and
pass a valid xml string as data
replace
httpRequest.ContentType = "application/x-www-form-urlencoded"

with
httpRequest.ContentType = "text/xml"

You could even go further and return an xml document if the expected datatype is
“text/xml”.

ASP.NET Membership and passwordStrengthRegularExpression

by Sebastien Aube

Membership in the .NET framework 2.0 allows you to add security to your application with little to no code.

When trying to enforce strong password rules in our church software I encounteredan interesting problem.

At first I modified the web.config by adding the following line to our membershipprovider section.

passwordStrengthRegularExpression=“(?=.{8,})[a-z]+[^a-z]+|[^a-z]+[a-z]+”

RegEx explained: 8 characters or more in length, at least 1 lowercase letter,at least 1 character that is not a lower letter.

I removed:

minRequiredPasswordLength=“0”
minRequiredNonalphanumericCharacters=“1”

After some testing I found that even when following the password rules, a passwordchange would fail.

The ChangePassword control, which is part of the Login suite of controls, doesn’tgive you any information as to why the password changed failed.

After a few reviews of my RegEx and confirming that the syntax is correct in codeand with some useful online regular expression testers (see links below), I triedchanging the password using the following code:

MembershipUser mUser = Membership.GetUser(); //gets the current logged in user
//change the password
mUser.ChangePassword(mUser.GetPassword(), “invalidpassword”);

That caused the following exception: System.ArgumentException: Non alpha numeric charactersin ‘newPassword’ needs to be greater than or equal to ‘1’.

So I added this line:

minRequiredNonalphanumericCharacters=“0”

And our password complexity rule started working properly.

I won’t start a debate on the merit of setting the minimum required non alphanumericcharacters (say that ten times) to 1, but hopefully this will help someone somewheresome time.

Links:

JavaScript Regular Expression Tester
http://www.roblocher.com/technotes/regexp.aspx

.NET Regular Expression Tester
http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx


How to check for a network connection in .NET 2.0 (C# or VB)


To check for a network connection in .NET 2.0 use this:


System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

To monitor a change in IP address or a change in network availability, use the events from the NetworkChange class:


System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged


System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged