Migrate to Windows 8 app

Nov 26 2012 7:20 AM

Hi All,
cc

I would like to convert simple webpage in C# to windows app

when i am trying to run the same code in VS 2012 Windows Store form it is giving me lot of errors.
I believe I need to change references but not sure how to do it?  

If someone can convert the following code. Thanks in Advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;


public partial class _Default : System.Web.UI.Page
{

    public static bool Validator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        HttpWebRequest request;
        HttpWebResponse response = null;
        StreamReader reader;
        StringBuilder sbSource;
        // 1. Set some variables specific to your account.
        string uri = "https://website.app.com/report";
        string username = "[email protected]";
        string password = "password";
        string usernamePassword = username + ":" + password;
        string msg = "";

        ServicePointManager.ServerCertificateValidationCallback = Validator;

        try
        {
            request = WebRequest.Create(uri) as HttpWebRequest;
            request.MaximumAutomaticRedirections = 1;
            request.AllowAutoRedirect = true;

            // 2. It's important that both the Accept and ContentType headers are
            // set in order for this to be interpreted as an API request.
            request.Accept = "application/xml";
            request.ContentType = "application/xml";
            request.UserAgent = "api_sample.cs";
            // 3. Add the Basic Authentication header with username/password string.
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

         
            using (response = request.GetResponse() as HttpWebResponse)
            {
                if (request.HaveResponse == true && response != null)
                {
                    reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    sbSource = new StringBuilder(reader.ReadToEnd());
                   
                     msg =  sbSource.ToString();
                     Label2.Text = sbSource.ToString();

                }
            }
        }
        catch (WebException wex)
        {
            if (wex.Response != null)
            {
                using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
                {
                    Label2.Text = "The server returned '{0}' with the status code {1} ({2:d}).", errorResponse.StatusDescription, errorResponse.StatusCode, errorResponse.StatusCode);
                }
            }
            else
            {
               Label1.Text=  wex.ToString();

            }
        }
        finally
        {
            if (response != null) { response.Close(); }
        }

 

    }
}





Thanks,

TM






 


 


Answers (2)