简体   繁体   中英

C# Http Post issue

I am having trouble with this code that worked before at one point.

    public static async Task<Response> PushTest() {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add( "Access-Token" , key ); //PROBLEM WAS HERE FOR SOME REASON
        //PROBLEM CODE
        //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Access-Token" , key );
        Uri url = new Uri(BaseUrl + "pushes");
        Dictionary<string,string> data =  new Dictionary<string, string>();
        data.Add( "body" , "TestBody" );
        data.Add( "title" , "TestTitle" );
        data.Add( "type" , "note" );
        data.Add( "email" , Email );
        HttpContent jsonContent = new StringContent(JsonConvert.SerializeObject(data, new KeyValuePairConverter()), System.Text.Encoding.UTF8,"application/json");

        HttpResponseMessage post = await client.PostAsync(url, jsonContent); // DEBUG POINT
        // WORKS NOW
        string responseString = await post.Content.ReadAsStringAsync();
        // DOESNT RETURN NULL TO THE MAIN METHOD ANYMORE
        return JsonConvert.DeserializeObject<Response>( responseString );
    }

    private static void Main( string[ ] args ) {
        Task<Response> n = PushTest();
        Response r = n.Result;
        Console.WriteLine( "Done" );
    } // DEBUG POINT
}

Summary:

Originally would not work because it would not make it past the post request, even with the better code for Tasks and async.

I don't know anything about Async or Tasks in C#, Only java. After fixing the code to the suggested fixes and focusing on Tasks, The actual problem was with the Authorization Header. Who knows why that is would be a better answer for me at this point. Switching to Add() instead fixed this issue. Will Research this.

PushTest() is a Task<T> so you wouldn't need to make a new Task out of the returned T (wait does that make sense?).

Try doing

Task<PostResponse> n = PushTest();
PostResponse r = n.Result;

Assuming you actually need the PostResponse because nowhere in your code are you actually using it (Hint: maybe try returning something ).

In your first attempt to set the header value, you used DefaultRequestHeader.Authorization to set the header value:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Access-Token" , key );

This will generate an Authentication Header with a scheme of Access-Token and the value of key as the auth token. The first argument of the AuthenticationHeaderValue ctor is the scheme, not the HTTP Header name.

Here is the resulting HTTP request assuming the key value is foo :

GET http://example.com/ HTTP/1.1
Authorization: Access-Token foo

But, based on your update to fix the code sample, where you use .Add() instead, it seems the web api that you are calling uses a custom HTTP Header called Access-Token instead of the standard Authorization header. So, when you do:

client.DefaultRequestHeaders.Add( "Access-Token" , key );

it generates an HTTP Header called Access-Token with the value of key . Again assuming key="foo" , this will generate the following HTTP Request:

GET http://example.com/ HTTP/1.1
Access-Token: foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM