0

I've attempted to send a POST request on a Windows Store App. And I've attempted to use Fiddler or Charles to capture it.

  • with Fiddler/Charles closed, everything works fine.
  • with Fiddler/Charles opened, PostAsync() raises an exception

Here is my attempt:

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;
        }
    }
}

Why I am unable to use fiddler or Charles to analyze the traffic? Here is the exception I get:

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
Juliano Alves
  • 2,006
  • 4
  • 35
  • 37
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

I found the origin of the issue and the solution: the Host header mustn't be manually defined for a request to work in an environment where Fiddler or Charles is active.

Which means we have to remove or comment-out the following line:

client.DefaultRequestHeaders.Host = uri.Host;

By not setting the Host youself, magically your app will be working again with a local proxy, and the framework will set the Host header fine by itself.

This will be useful for people who might have followed this recent Microsoft's guide where they do it wrong: http://blogs.msdn.com/b/wsdevsol/archive/2013/02/05/how-to-use-httpclient-to-post-json-data.aspx

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Fascinating. This may come from the horrific pattern of trying to support using the HOST header to control the DNS resolution independent of the URL's host (which is illegal under RFC2616). Following up with the .NET team. – EricLaw May 23 '13 at 15:56
  • Do you know why the request *failed* though? Does your environment require some other proxy? – EricLaw May 23 '13 at 15:57
  • @EricLaw best I can provide is the Exception details from the question. No, I don't have any other proxy, and windows firewall was shut off. And a friend even tried on a different computer and it was the same issue. – Cœur May 24 '13 at 12:00
  • Thank you for the solution!! Was knocking my head against the wall trying to figure out why it worked without Fiddler running! – blizz Jul 19 '13 at 20:52
0

Did you forget to grant your application an AppContainer Loopback exemption?

Did you forget to configure the machine to trust Fiddler's root certificate?

Ryan B
  • 3,364
  • 21
  • 35
EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • I didn't forget any of those two settings. The issue origin was somewhere else: configuring `DefaultRequestHeaders.Host` yourself seems forbidden if you need a local proxy. See my answer. – Cœur May 23 '13 at 14:18