【解決方法】Web サイトにログインするための POST リクエストの送信

プログラミングQA


こんにちは、MSDN の例に基づいて Web サイトにログインするための POST 要求を送信しようとしています。次のコードがあります。

C#
WebRequest request = WebRequest.Create("http://kanbanize.com/index.php/api/kanbanize/login/email/test1@gmail.com/pass/test1");

// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//string postData = "This is a test that posts this string to a Web server.";
string postData = "This is a login test";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

私の設定にも次の設定を追加しました

XML
<system.net>
    <defaultProxy>
      <proxy usesystemdefault="False" bypassonlocal="True"/>
    </defaultProxy>
  </system.net>

しかし、これを実行しようとすると、「リモート サーバーに接続できません」というエラーが表示されます。

解決策 1

やあ、

リクエストとともに API キーを投稿する必要があります。 API キーは Request Body で提供する必要があります。

URL : http://kanbanize.com/index.php/api/kanbanize/login/email/test1%40gmail.com/pass/test1

API キーを追加する必要があります

引用:

API キー

システムに対して自分自身を認証するには、API キーをヘッダーとして指定する必要があります。 このようなキーを生成するには、システムにログインし、[マイ アカウント]->[API]パネルに移動します。 役割で許可されている場合は、タブにアクセスして一意のキーを生成できます。

注: ヘッダー名は「apikey」である必要があります。

詳細については、このページを参照してください。 https://kanbanize.com/ctrl_integration[^]

次に、exe を実行している PC からその URL に接続できるかどうかを確認します。
はいの場合: 次に、チェック 1. プロキシが必要か、またはファイアウォールが存在するかどうかを確認します。

スタックした場所を比較するには、 Fiddler を使用できます。 http://www.telerik.com/fiddler[^]

そして、ブラウザを使用して、Exe を使用して 2 つの要求を比較します。

コメント

タイトルとURLをコピーしました