如何使用HTTP帖子发送希腊字符?

时间:2014-12-19 21:05:14

标签: java objective-c json http utf-8

我使用以下代码在目标c中创建了一个请求:

NSString *urlBasic = [NSString stringWithFormat:@"http://localhost:8080/belka/Login?"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[defaults objectForKey:@"userID"], @"userID", @"login", @"login", [defaults objectForKey:@"userName"], @"userName", [defaults objectForKey:@"userFirstName"], @"userFirstName", [defaults objectForKey:@"userLastName"], @"userLastName", nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSUTF8StringEncoding
                                                         error:&error];
NSString *s=[urlBasic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:s];
dispatch_async(kBgQueue, ^{
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:jsonData];
        NSURLResponse *response;
        NSError *error;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if ((data.length > 0) && (error == nil)) {
            [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:NO];
        }
    });

在默认值中,userFirstName和userLastName在希腊语中。当我打印“jsonData”描述时,希腊字符是可以的。当我打印http body我得到:

  

<7b227573 65724669 7273744e 616d6522 3a22ce9c ceb1cebd cf8ccebb ceb7cf82 222c226c 6f67696e 223a226c 6f67696e 222c2275 7365724c 6173744e 616d6522 3a224b61 72616d61 6e697322 2c227573 65724944 223a2231 30313532 38313637 33333732 33313638 222c2275 7365724e 616d6522 3a224d61 6e6f6c69 73204b61 72616d61 6e697322 7d>

发送请求后,在服务器端,在java中,我有:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    StringBuffer jb = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
          jb.append(line);
      } catch (Exception e) { /*report an error*/ }

    try
    {
        InitialContext ctx2 = new InitialContext();
        UserTransaction utx = (UserTransaction) ctx2.lookup("java:module/UserTransaction");
        utx.begin();
        JSONObject data = new JSONObject(jb.toString());
        String facebookID = data.getString("userID");
        String name = data.getString("userFirstName");
        String surname = data.getString("userLastName");
        String username = data.getString("userName");
        System.out.println(name);
        //and other . . . 
        utx.commit();
        response.setCharacterEncoding("UTF-8");
        response.getOutputStream().write(name.getBytes());
    }   
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

问题是,当我尝试打印名称时,我得到这个:ÎÎÎÎÏÏ»Î·Ï 并且此名称将作为响应发送回ios设备,如下所示:      response =“\ U00ce \ U009c \ U00ce \ U00b1 \ U00ce \ U00bd \ U00cf \ U008c \ U00ce \ U00bb \ U00ce \ U00b7 \ U00cf \ U0082”; 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

错误必须位于链的最后一位,此处未显示的代码段打印响应并使您认为传输已损坏。

iOS客户端和Java服务器可以互相理解,实际上响应是Μανόλης(我不会说希腊文,但它似乎是你要找的文字)

您看到的文本ÎανÏληÏ是与7位编码相同的字节序列(可能是CP1252,也就是拉丁语1或CP1253,希腊语代码页,我无法分辨,因为似乎有StackOverflow字体的问题,或者无论如何我的测试程序都无法产生输出)

Here is the program我曾经分析过您在问题结尾处发布的“错误”回复