Alright, so I'm writing a program to access a RESTful service, but I first must provide authentication to the website. I have valid credentials and my entire code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace AccessAPI
{
class Program
{
static void Main(string[] args)
{
string uri = "http://domainName/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string auth = CreateAuthorization("http://domainName/", "my\\username", "password");
request.Headers["Authorization"] = "Basic " + auth;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
static string CreateAuthorization(string realm, string userName, string password)
{
string auth = ((realm != null) && (realm.Length > 0) ?
realm + @"\" : "") + userName + ":" + password;
auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
return auth;
}
}
}
I've come this far using a few of the solutions posted on other forums. While this seemed to work out for some people, it doesn't for me. First off, I do KNOW that the credentials are accurate. I'm able to enter them over a web browser and they behave just as they should. Also, I made sure that any "\" in them (there is one in the username) is represented with a "\\" in the code. Every time I run this, though, I keep on getting back "The remote server returned an error: (401) Unauthorized." Can anyone help me troubleshoot through this? Thank you for your time!
Aucun commentaire:
Enregistrer un commentaire