2012-07-28 6 views
0

Net :: HTTP를 통해 데이터를 가져와야하는데, ASCII로 응답을받는 것이 좋습니다. 8 비트. 문제는이를 utf8로 인코딩하고 비 라틴계 심볼을 모두 저장하는 방법입니다. @content.encode('utf-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')Net :: HTTP.get_response.body의 비 라틴계 (키릴 문자) 기호를 사용하는 RoR ASCII-8bit

나는 모든 키릴 문자가

@content.encode('utf-8', 'binary')

내가 키릴 문자 대신 얻을 @content.force_encoding("UTF-8)

"\xCB" from ASCII-8BIT to UTF-8 오류가 느슨한

나는에 대한 답변을 찾을 수없는 구글 검색.

답변

3

문제

begin 
    cleaned = response.body.dup.force_encoding('UTF-8') 
    unless cleaned.valid_encoding? 
     cleaned = response.body.encode('UTF-8', 'Windows-1251') 
    end 
    content = cleaned 
rescue EncodingError 
    content.encode!('UTF-8', invalid: :replace, undef: :replace) 
end 

here is more complete data

으로 해결
관련 문제