Tuesday, October 21, 2014

Catch WebException from HttpWebRequest POST error response.

A WebException will be thrown if a non 200 status code is returned from the server.  To check the message returned, catch it and read the response.


try
{ 
  byte[] buf = Encoding.UTF8.GetBytes(payload);
  httpReq.ContentLength = buf.Length;
  httpReq.GetRequestStream().Write(buf, 0, buf.Length);
 
  HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();

   if (resp.StatusCode != HttpStatusCode.NoContent && resp.StatusCode != HttpStatusCode.NotFound)
  {   
    return resp.GetResponseStream(); //Successful response
  }
}

catch (WebException wex)
{ 
  using (var stream = ex.Response.GetResponseStream())

  using (var reader = new StreamReader(stream))
  {   

    Console.WriteLine(reader.ReadToEnd()); //Message returned from client
  }

}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached

}
 

Friday, October 17, 2014

Allow HTTP GET and POST of data to an .aspx page.

In Page_Load, use Request directly to allow GET and POST of data from client.

Request.QueryString[] will return value from the GET request of query string. 
i.e., http://[url]?data=somedata
Request.QueryString["data"] //will return "somedata"

Request.Form[] will return value of POST request.
i.e.,

<form action="http://[url]/Default.aspx" method="post">
    <input name="data" type="text" value="somedata" id="data" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>
Request.Form["data"] //will return "somedata"

Request[] will look through all the collections below to find a match:
HttpRequest.QueryString
HttpRequest.Form
HttpRequest.Cookies
HttpRequest.ServerVariables

i.e., if Request["data"] is used, the page will accept GET and POST request without specifying which specific method the page will allow.

Wednesday, October 1, 2014

How to fix kubuntu that halts while booting on battery

To fix a machine that freezes during boot when its on battery, follow this:

1.) Edit grub and reboot the machine
     a.) sudo nano /etc/default/grub
     b.) add nolapic between the quotes on the line GRUB_CMDLINE_LINUX=""
     c.) sudo update-grub


The test temporarily, enter into grub during boot up:
1.) Hold left Shift button during boot
2.) Press the 'e' button
3.) Edit the Linux line to include the option acpi=no
4.) Optionally, remove the "quite splash" option from the Linux line to see ubuntu boot up *
5.) Hit Ctrl+x to reboot

*This will also help debug the problem if these instructions don't fix the problem.