0

JSON 応答を送信する REST API に接続しようとしています。ブラウザに URL をコピーして貼り付けると、ブラウザにユーザー名とパスワードを入力するためのポップアップが表示されます。

ユーザー名とパスワード (Active Directory ID とパスワード) を入力すると、サービスは JSON 応答を送信します。 HTTP リクエストとレスポンス

しかし、コードから接続しようとすると、次のエラーが返されます

{StatusCode: 401、ReasonPhrase: 'Unauthorized'、バージョン: 1.1、コンテンツ: System.Net.Http.StreamContent、Headers: { Keep-Alive: timeout=15、max=100 Connection: Keep-Alive Date: Wed, 20 Apr 2016 11:38:45 GMT サーバー: Apache WWW-Authenticate: Basic realm="Login using your AD-ENT credentials, but do not do your ID with the domain"
Content-Length: 455 Content-Type: text/html; charset=iso-8859-1 }}

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(@"http://pro.abc.com/services/");

var authData = string.Format("{0}:{1}","username", "password");
var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);

HttpResponseMessage response = await client.GetAsync("reportpro/reports/11824");
4

1 に答える 1

0

コードは完璧でした。唯一の問題は、https ではなく http を使用していたことです。

URL をブラウザーにコピーして貼り付けると、ブラウザーは http を https に変換していました。したがって、ブラウザで完全に機能しました。

コードで http を https に変更すると、完全に機能しました。

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(@"https://pro.abc.com/services/");
于 2016-04-20T12:24:55.967 に答える