Thursday, June 11, 2015

Getting WebException with full response body

Di load langsung...
   try
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            string content = client.DownloadString("https://sandiegodata.atlassian.net/wiki/pages/doaddcomment.action?pageId=524365");
            Console.WriteLine(content);
            Console.ReadKey();
        }
        catch (WebException erro)
        {
            string webpageContent  = ""; 
            if( erro.Response == null )
                  {
                   webpageContent = "Error:"+erro.Status;
                  }
                  else if( erro.Response != null)
                  {
                    var resp = new StreamReader(erro.Response.GetResponseStream()).ReadToEnd();
                   
                }
                
        }
with json.net
  catch (WebException ex)
        {
if( a.Response == null )
                  {
                   webpageContent = "Error:"+a.Status;
                  }
                  else if( a.Response != null)
                  {
                var resp = new StreamReader(a.Response.GetResponseStream()).ReadToEnd();

                dynamic obj = JsonConvert.DeserializeObject(resp);
                var messageFromServer = obj.error.message;
                var codemessage = obj.error.code;
                string mesageex = a.Message;
                webpageContent = "Error:"+codemessage.Value + ":" + messageFromServer.Value;
                  }
}