简体   繁体   中英

How to post a numeric value in c#

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 ac#/.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();
}

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 &

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