如何编码以获得所需的结果?

时间:2012-08-06 23:49:57

标签: c# winforms visual-studio-2010 encoding

原始字符串是:

    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);

我正在获取字符串(我在fiddler中看到),如:

    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);

仍然没有运气..

谢谢

1 个答案:

答案 0 :(得分: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();