2017-10-29 10 views
0

私はget要求を実装するためのコンソールをビルドするためのコードをC#で作成しています。 私たちはPOSTMANを使用して作業を行っていましたが、独自のPOSTMANを構築することにしました。C#の承認ヘッダーとコンテンツタイプ

リクエストのヘッダーにユーザー名とパスワードを渡す必要があります。 プログラミングに慣れていないので、私を導くことができますか?私はコードの下に書かれている

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net.Http; 
namespace ConsoleApp2 
{ 
    class Program 
    { 
     private static object response; 

     static void Main(string[] args) 
     { 
      GetRequest("https://www.google.com"); 
      Console.ReadKey(); 
     } 
     async static void GetRequest(string url) 
     { 
      using (HttpClient client = new HttpClient()) 
      { 

       using (HttpResponseMessage response = await client.GetAsync(url)) 
       { 
        using (HttpContent content = response.Content) 
        { 
         string mycontent = await content.ReadAsStringAsync(); 
         Console.WriteLine(mycontent); 
        } 
       } 
      } 
     } 

    } 
} 
+0

のためにスニペットの下に使用してきましたURLは...郵便配達員で動作しましたか?HttpClientでヘッダーとアプリケーションタイプはどこで設定していますか? –

+1

あなたがそれを求めているのではなく、答えを探す時間を取ったなら、それは素晴らしいことでした。 –

答えて

0

あなたはコードスニペットに

HttpClient client = new HttpClient() 
var byteArray = Encoding.ASCII.GetBytes("username:password1234"); 
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
0

DefaultRequestHeadersを以下試すことができますが動作していない、私が何であるかを認証

string _ContentType = "application/json"; 
httpWebRequest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType)); 
var _CredentialBase64 = "xxxxxxxxx="; 
httpWebRequest.DefaultRequestHeaders.Add("Authorization", String.Format("Basic {0}", _CredentialBase64)); 
関連する問題