0

Windows ストア アプリで POST 要求を送信しようとしました。そして、私はそれをキャプチャするために Fiddler または Charles を使用しようとしました。

  • Fiddler/Charles を閉じると、すべて正常に動作します。
  • Fiddler/Charles を開いた状態で、PostAsync()で例外が発生する

これが私の試みです:

Uri uri = new Uri("http://example.com");
using (StringContent content = new StringContent("{}", Encoding.UTF8, "application/json"))
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Host = uri.Host;
        try
        {
            using (HttpResponseMessage response = await client.PostAsync(uri, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    String result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                return null;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

fiddler または Charles を使用してトラフィックを分析できないのはなぜですか? これが私が得る例外です:

Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
HelpLink    null    string
HResult -2146233088 int
InnerException  {"The underlying connection was closed: Unable to connect to the remote server."}   System.Exception {System.Net.WebException}
IPForWatsonBuckets  206848215   System.UIntPtr
IsTransient false   bool
Message "An error occurred while sending the request."  string
RemoteStackTrace    null    string
Source  "mscorlib"  string
StackTrace  "   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Example.Services.ExampleServices.<ExampleClass>d__3.MoveNext() in c:\\TFS\\Example\\ExampleMain\\Example\\Services\\ExampleServices.cs:line 110"   string
TargetSite  {Void Throw()}  System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
WatsonBuckets   null    object
4

2 に答える 2

1

問題の原因と解決策を見つけました。Fiddler または Charles がアクティブな環境で機能するリクエストに対して、Host ヘッダーを手動で定義してはなりません。

つまり、次の行を削除またはコメントアウトする必要があります。

client.DefaultRequestHeaders.Host = uri.Host;

Host を自分で設定しないと、魔法のように、アプリはローカル プロキシで再び動作し、フレームワークはそれ自体で Host ヘッダーを適切に設定します。

これは、最近の Microsoft のガイドに従って間違ったことをしている可能性のある人に役立ちます 。 to-post-json-data.aspx

于 2013-05-23T14:16:37.863 に答える
0

アプリケーションにAppContainer Loopback の免除を付与するのを忘れていませんか?

Fiddler のルート証明書を信頼するようにマシンを構成するのを忘れていませんか?

于 2013-05-22T19:10:26.770 に答える