Tuesday, March 19, 2013

Set the HTML form with data from in the page's address


Use JQuery to fill in a Web form when post-back to the page using method="get".

Assume the a Web page has the following address http://localhost:/Example01.htm and contains the following simple HTML form:


The source code of that form may appear as following:

<form action="Example01.htm" method="get">

  <input id="QueryString" type="text" name="QueryString" />

  <select id="Method" name="Method">
    <option value="And">All Words</option>
    <option value="Or">Any Word</option>
  </select>

  <input type="submit" value="Submit" />

</form> 


Upon submit of the form, which references the current page in the forms action parameter, the page's address is updated with the values from the QueryString and Method input fields:

http://localhost:/Example01.htm?QueryString=my+query+terms&Method=And

The following JavaScript parses the address parameters to re-set the form with the data provided by the user:



<script src="jquery-1.9.1.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">


$(document).ready(function () {

    var vQueryString = getURLParam("QueryString");
    $("#QueryString").val(vQueryString);

    var vMethod = getURLParam("Method");
   $("select#Method option").each(function () {
 if ($(this).val() == vMethod) {
     $(this).prop('selected', true);
 }
    });

});

function getURLParam(name){
 return decodeURIComponent((new RegExp('[?|&]' +
    name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || 
    [, ""])[1].replace(/\+/g, '%20')) || null;
}
</script>

Friday, March 1, 2013

REST calls in CSharp

I needed to code set of REST calls in C# with a some specific requirements that prevented use of simpler WebClient API:


  • Setting the ContentType for "text/xml"
  • Support for GET, DELETE, and POST methods
  • The POST method had to submit data (an XML fragment) and also returned data (XML fragment)

Following is what I hacked out and documenting here so I don't have to look it up again.


XmlDocument RestGet(string uri)
{
    XmlDocument doc = new XmlDocument();

    HttpWebRequest request = 
     (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.ContentType = "text/xml";

    HttpWebResponse response = 
     (HttpWebResponse)request.GetResponse();
    doc = new XmlDocument();
    doc.Load(response.GetResponseStream());
    response.Close();

    return doc;
}



static XmlDocument RestDelete(string uri)
{
    XmlDocument doc = new XmlDocument();

    HttpWebRequest request = 
     (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "DELETE";
    request.ContentType = "text/xml";

    HttpWebResponse response = 
     (HttpWebResponse)request.GetResponse();
    doc = new XmlDocument();
    doc.Load(response.GetResponseStream());
    response.Close();

    return doc;
}


static XmlDocument RestPost(string uri, string data)
{
    XmlDocument doc = new XmlDocument();

    HttpWebRequest request = 
     (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "text/xml";

    System.Text.UTF8Encoding encoding = 
     new System.Text.UTF8Encoding();
    byte[] byte1 = encoding.GetBytes(data);
    request.ContentLength = byte1.Length;

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(byte1, 0, byte1.Length);
    requestStream.Close();

    HttpWebResponse response = 
     (HttpWebResponse)request.GetResponse();
    doc = new XmlDocument();
    doc.Load(response.GetResponseStream());
    response.Close();

    return doc;
}

Wednesday, November 14, 2012

telnet www.yourdomain.com 80

Assuming you want to view the raw HTTP response from a Web page at http://www.yourdomain.com/index.html you can use the following telnet syntax (example from dos prompt, note the '.' blank line).

telnet www.yourdomain.com 80
GET /index.html
host:
www.yourdomain.com
.

Note that this does not support NTLM with IIS servers for those enterprise users.

Sunday, November 4, 2012

Hurricane Sandy

Like most New York'ers (or at least those of us living in and around the big apple) I was hit by Hurricane Sandy.

Unlike many I was only inconvenienced by the storm rather than uprooted.  My co-op building was well above sea level and not at risk to the rising water. We did lose power and water for five days which was very inconvenient but not in itself life threatening; I’d stored up plenty of food and drinking and flushing water. For me it was the equivalent to going camping in my apartment.


My nightlight during the blackout

I do admin to some apprehension on the thought that one of my neighbors would knock over a candle and burn the block to the ground.  All throughout the dark night fire trucks would be rushing off to the next crises while I hoped they would pass my block bye. Such are the fears of a country boy living in the crowded city.

It was very odd after the storm passed and the city rushed back to “normal”.

Power was out south of 39th street, in my neighborhood, and so stores were closed, street lights dark, and people were bucketing water up to their cold apartments. During the days, they would wandered the post apocalyptic streets looking for coffee or a WiFi hot spot. We jokingly called this area SoNoPow - for South with No Power. 

North of that business were open and people rushed around and complained about the traffic delays as if those were personal attacks against their dignity. Through this I may have got a bit of perspective of what a homeless person might feel, on how out of touch the iPad and latte world was with someone who’s priority it was to find food and shelter for the night. And how that other world really doesn't want to think about those who are in need when they can focus on any threat to their needs.


Monday, August 27, 2012

Leather Three-Pocket Fairing Pouch




Leather Three-Pocket Fairing Pouch

My sister, who is a big motorcyclist herself, gave me the above three pocket fairing pouch for Christmas.  When I replaced my windshield with the extra tall version (trying to reduce some of the buffeting doing down the highway) it was a perfect opportunity to install the pouch and, no shown here, a nice chrome accent strip.