2012-08-06 56 views
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); 

仍然没有运气..

谢谢

回答

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(); 
+0

我如何把它分配给字节数组之后呢? – confusedMind 2012-08-07 00:18:17

+0

byte [] buffer = Encoding.UTF8.GetBytes(result); – bmode 2012-08-07 00:22:42

+0

我懂了:)它的工作完美...我用System.Text.ASCIIEncoding编码=新System.Text.ASCIIEncoding(); postData = postData.Remove(0,2); Byte [] byteData = encoding.GetBytes(postData); 因为它是在开始时加入&=。 – confusedMind 2012-08-07 00:51:20