0

I'm trying to use HTTPclient to post to a 3rd party api and get a response. I'm able to post to the api but the problem i'm having is that the api requires the programid to be numeric. Whenever i try to post with this code it throws it back telling me to make the programid numeric.

I'm at a loss as to how to send a numeric via a c#/.net post

using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
    {
       { "accesstoken", authtoken },
       { "programid", programid},
       { "email", person.Email },
       { "thirdparty1", person.ID.ToString() }
    };

    var content = new FormUrlEncodedContent(values);
    var response = client.PostAsync("url", content);
    var responseString = response.Result.Content.ReadAsStringAsync();
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
johnlxc
  • 11
  • 1
  • 3
  • 2
    What is the type and value of `programid`? Also have you cased it correctly if the client api you are sending it to is case sensitive on the input? – Igor Jul 29 '16 at 19:43
  • http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp – Griffin Jul 29 '16 at 19:43
  • right now the type is a string in c# but the value is 192926. The api is expecting a numeric value. but i am having trouble formating the value as a int and passing it into formurlencodedurl – johnlxc Jul 29 '16 at 19:55
  • can you use fiddler to see what you are passing, and determine if it is in fact a number? – 2174714 Jul 29 '16 at 20:49
  • 1
    http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp – 2174714 Jul 29 '16 at 20:55

1 Answers1

0

maybe WebClient?

using (var client = new WebClient())
            {
                var values = new Dictionary<string, string>
    {
       { "accesstoken", authtoken },
       { "programid", programid.ToString()},
       { "email", person.Email },
       { "thirdparty1", person.ID.ToString() }
    };

                var param = string.Join("&", values.Select(x => x.Key + "=" + x.Value).ToArray());

                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string HtmlResult = client.UploadString(URI, "POST", param);
            }

just make sure you aren't passing any funny url routing characters like ? or &

2174714
  • 288
  • 2
  • 10