简体   繁体   English

c#httpclient POST请求:无法发送特殊字符

[英]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). 我有一个应该登录到站点的程序,它使用POST请求,并且一切正常,直到其中一个值包含特殊字符(例如'%')为止。

    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 我虽然需要“警告”服务器abaut编码,所以我添加了

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. 内容类型和方式请求应该看起来像我从Firebug那里获得的,所以,如果我在那里能找到一些答案,请指出。

modify0: Also, i compared what my program send to server with browser request(using Firebug) and my request is completley same. Modify0:另外,我比较了我的程序与浏览器请求(使用Firebug)发送到服务器的内容,并且我的请求完全相同。 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. Modify1:另外,当我在浏览器中检查服务器时,服务器处理特殊字符没有问题。 For example it(browser) sent "K&YF82" as "captcha_text=K%26YF82"(same value in addres propereties and request body) and all worked fine. 例如,它(浏览器)以“ captcha_text = K%26YF82”(地址属性和请求正文中的值相同)发送“ K&YF82”,并且一切正常。 UrlEncode do same replacement, but in my program it doesnt get accepted by server. UrlEncode做同样的替换,但是在我的程序中它没有被服务器接受。

SOLUTION: 解:

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

insteat of 的实例

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

Are you dealing with REST application?? 您正在处理REST应用程序吗?

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 还可以查看以下文章中的堆栈文章: 特殊字符构成REST Web服务通信的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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