0

原始字符串是:

    csrfToken=ajax:1238044988226892967&postTitle=Job Openings Linux Systems Administrator 
Staff&postText=Security Clearance: Public Trust -- Linux systems administration experience specifically in managing or supporting RedHat and/or Centos Linux in...&pollChoice1-
ANetPostForm=&pollChoice2-ANetPostForm=&pollChoice3-ANetPostForm=&pollChoice4-ANetPostForm=&pollChoice5-ANetPostForm=&pollEndDate-ANetPostForm=0&contentImageCount=1&contentImageIndex=0&
contentImage=http://www.ideal-jobs.net/images/image070.jpg&contentEntityID=5637974394992087135&contentUrl=http%3a%2f%2fwww.ideal-jobs.net%2fjob-openings-
linux-systems-administrator-staff%2f&contentTitle=Job Openings Linux Systems Administrator 
Staff&contentSummary=Security Clearance: Public Trust -- Linux systems administration experience specifically in managing or supporting RedHat and/or Centos Linux in...&contentImageIncluded=true&%23=Save&tweet=&postItem=Share&gid=50565&ajax=true&tetherAccountID=&facebookTetherID=

字符串我希望它在编码后是这样的:

 csrfToken=ajax%3A6293994705950333071&postTitle=hello&postText=Hi%20everyone%20hae%20a%20good%20day%20%2C%20i%20am%20new%20to%20this%20%3A)&
pollChoice1-ANetPostForm=&pollChoice2-ANetPostForm=&pollChoice3-ANetPostForm=&pollChoice4-
ANetPostForm=&pollChoice5-ANetPostForm=&pollEndDate-ANetPostForm=0&contentImageCount=0&contentImageIndex=-1&contentImage=&contentEntityID=&contentUrl=&contentTitle=&
contentSummary=&contentImageIncluded=true&%23=&gid=163857&postItem=&ajax=true&tetherAccountID=&facebookTetherID=

目前我正在使用:

byte[] byteData = HttpUtility.UrlEncodeToBytes(postData);

我得到了这样的字符串(我在提琴手中看到):

    csrfToken%3dajax%3a1238044988226892967%26postTitle%3dJob+Openings+Linux+Systems+Administrat
or+Staff%26postText%3dSecurity+Clearance%3a+Public+Trust+--+Linux+systems+administration+ex
perience+specifically+in+managing+or+supporting+RedHat+and%2for+Centos+Linux+in...%26pollCh
oice1-ANetPostForm%3d%26pollChoice2-ANetPostForm%3d%26pollChoice3-ANetPostForm
%3d%26pollChoice4-ANetPostForm%3d%26pollChoice5-ANetPostForm%3d%26pollEndDate-
ANetPostForm%3d0%26contentImageCount%3d1%26contentImageIndex%3d0%26contentImage%3dhttp
%3a%2f%2fwww.ideal-
jobs.net%2fimages%2fimage070.jpg%26contentEntityID%3d5637974394992087135%26contentUrl%3dhtt
p%253a%252f%252fwww.ideal-jobs.net%252fjob-openings-linux-systems-administrator-
staff%252f%26contentTitle%3dJob+Openings+Linux+Systems+Administrator+Staff%26contentSummary
%3dSecurity+Clearance%3a+Public+Trust+--+Linux+systems+administration+experience+specifical
ly+in+managing+or+supporting+RedHat+and%2for+Centos+Linux+in...%26contentImageIncluded%3dtr
ue%26%2523%3dSave%26tweet
%3d%26postItem%3dShare%26gid%3d50565%26ajax%3dtrue%26tetherAccountID
%3d%26facebookTetherID%3d

还尝试过:

UTF8Encoding encoding = new UTF8Encoding();

byte[] byteData = HttpUtility.UrlEncodeUnicodeToBytes(postData);

还是没有运气。。

谢谢

4

1 回答 1

0

问题是您正在对整个字符串进行 URL 编码,包括分隔符 & 和 =。您首先需要将字符串解析为字段,然后对字段名称和值进行 url 编码,最后重新组合成一个字符串。

试试这个:

string input;      // Your input string
List<string> outputs = new List<string>();

// Parse the original string
NameValueCollection parms = HttpUtility.ParseQueryString(input);

// Loop over each item, url encoding
foreach (string key in parms.AllKeys)  {
    foreach (string val in parms.GetValues(key))
        outputs.Add(HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(val));
}

// combine the encoded strings, joining with &
string result = string.Join("&", outputs);    // the final result

编辑

这是我在尝试以前的想法时发现的一个更简单的版本:

string result = HttpUtility.ParseQueryString(postData).ToString();
于 2012-08-07T00:09:24.833 回答