简体   繁体   中英

c# httpclient POST request: cant send special characters

I have a program that should login to site, it uses POST requests, and all goes fine, until one of the values contain special character('%' for example).

    captcha = "ABCDE" //all goes fine and well, server accept captcha
    captcha = "ABC&%" //server dont accept captcha and return fail

    //here is the bad part:
    string request = "password=" + HttpUtility.UrlEncode(encpass, Encoding.UTF8) +
 "&username=" + login + "&captcha_text=" + HttpUtility.UrlEncode(captcha, Encoding.UTF8);

Also, i ofcourse googled it, and checked all i could find. I though i need to "warn" server abaut encoding, so i added

request.Headers.TryAddWithoutValidation("Content-Type", @"application/x-www-form-urlencoded; charset=UTF-8");

but it still did not helped me.

Content types and way request should look like i get from Firebug, so if i can find some answers there - please point.

modify0: Also, i compared what my program send to server with browser request(using Firebug) and my request is completley same. Only difference - my request dont get accepted in values it contain special-characters.

modify1: Also, server have no problems handling special-characters when i check it in browser. For example it(browser) sent "K&YF82" as "captcha_text=K%26YF82"(same value in addres propereties and request body) and all worked fine. UrlEncode do same replacement, but in my program it doesnt get accepted by server.

SOLUTION:

{ password:"df464dsj", username:"username", captchaText:"ABC&%", remember_login:"false" }

insteat of

password=f2341f14f&username=username&captha...

Are you dealing with REST application??

If yes then send your post data in request body instead of query string.

Also have a look at the stack post at : Special characters pose problems with REST webservice communication

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