← Back to team overview

dhis2-users team mailing list archive

DHIS2 Web Api - using C#

 

Hi,

Can anyone have an example of an established connection to DHIS2 using C#
HttpWebRequest or WebClient methods?

I have written this method and all i get are errors when establishing the
connection.

private void ConnectDHIS()
        {
            string uname = "admin", pwd = "district";
            string formattedUrl = "
https://apps.dhis2.org/demo/dhis-web-commons/security/login.action?authOnly=true
";
            string credentials =
Convert.ToBase64String(Encoding.ASCII.GetBytes(uname + ":" + pwd));
            HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(formattedUrl);
           // request.PreAuthenticate = true;
            request.Headers[HttpRequestHeader.Authorization] =
string.Format("Basic {0}", credentials);
           // request.Headers["Authorization"] = "Basic " + credentials;
            //request.Headers.Add("Authorization","Basic " + credentials);
            HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
            txtresponse.Text = response.ResponseUri.ToString();
        }

The above code does not work even for my own local instance of DHIS2.

Charles.

Follow ups