Ruby [已解决] Ruby 脚本中中文字符的问题 (模拟 POST 提交表单)

tylr · 2012年05月08日 · 最后由 shabaoma 回复于 2014年01月16日 · 8755 次阅读

我正在照着如下这段范例代码模拟 POST 提交表单数据,遇到了中文字符的问题,一到含有中文字符的那行就会被提示 C:\Users\tylr\Desktop>ruby auto_post.rb auto_post.rb:11: invalid multibyte char (UTF-8) auto_post.rb:11: invalid multibyte char (UTF-8) auto_post.rb:11: syntax error, unexpected $end, expecting '}' "ctl00" => "测试",

我尝试了在脚本行首加入#encoding:utf-8,但也没有帮助,还望得到大家指点,谢谢先!

范例代码: require "uri" require "net/http"

params = {'box1′ => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post', 'button1′ => 'Submit' } x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)

匿名 #1 2012年05月08日

脚本文件的编码是不是 utf-8 的?

#1 楼 @jjym 果然!谢谢 jjym 的提醒!改了后就好啦~

匿名 #3 2012年05月08日

当初自己找这 bug 找了好久...

#1 楼 @Guest 请问,我现在用的 ruby 是 1.8.6,写中文就是乱码,脚本文件存的是 ANSI, 如果存成 UTF-8 就会报 undefined method `∩╗┐require' for main:Object (NoMethodError) Invalid char '/357' in expression Invalid char '/273' in expression Invalid char '/277' in expression 不知道该如何处理?希望你指点指点!

#4 楼 @doristian 1 用 ANSI 编码 或 2 用 Ruby1.9 或 3 Ruby 解释器 -u 选项

#5 楼 @jjym

  1. 如果用 ANSI 保存,中文写到页面中就是乱码。
  2. 现在不能用 ruby 1.9, 只能用 ruby1.8.6
  3. 我使用了 ruby -Ku test.rb, 将中文写到页面上还是乱码。 请看http://ruby-china.org/topics/4397 谢谢

我也遇到了这个问题,看了这个帖子才恍然大悟!谢了!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号