希伯来语在服务器端被视为乱码

发布于 2024-10-31 13:18:18 字数 3462 浏览 3 评论 0原文

我有一个 iPhone 模拟器应用程序作为客户端和 WCF-REST,在 IIS 7.0 服务器上运行的 json 编码服务。 我试图将一些希伯来字符作为参数发送到服务,服务器返回错误代码 400 - 错误请求。 当我发送英文字符串时,服务器会按其应有的方式处理该字符串。

用于字符串编码的代码如下所示:

请注意,“parameters”是要发送到服务器的参数,排序为parameter_name =>参数值。

//RPC JSON
NSString* reqString = [parameters JSONRepresentation];
//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];
//prepare http body
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

我在服务器端收到的请求和响应(通过使用 ethereal 得到的):

请求:

POST /SmsliMobileService.svc/SetContactGroups HTTP/1.1
Host: 192.168.10.22:8081
User-Agent: Beeper_3/1.0 CFNetwork/485.12.7 Darwin/10.7.0
Content-Length: 89
Accept: application/json
Content-Type: application/json; charset=UTF-8
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive

响应:

{"ApplicationID":"98534bb6-82ef-4937-83e7-10f65780bf36","contact_id":43,"groups":"עבו×HTTP/1.1 400 Bad Request
Content-Length: 1647
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sun, 10 Apr 2011 07:46:09 GMT

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-    left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-    bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; }     A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active {     color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-    color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-    size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-    left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding:     5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-    wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse;     border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid;     border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td {     border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}    </style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for     more details.</p>
    </div>
  </body>
</html>

任何帮助将不胜感激。

I have an iPhone simulator app as client and WCF-REST, json encoded service running on IIS 7.0 server.
I'm trying to send some hebrew characters to the service as parameter and the server return error code 400 - Bad Request.
When I'm sending an English string the server processes the string as it should.

The code used for the string encoding looks like this:

Note that "parameters" is the parameters that about to be sent to the server, ordered as parameter_name => parameter_value.

//RPC JSON
NSString* reqString = [parameters JSONRepresentation];
//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];
//prepare http body
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

The request and response I'm getting in the server side (got this by using ethereal):

Request:

POST /SmsliMobileService.svc/SetContactGroups HTTP/1.1
Host: 192.168.10.22:8081
User-Agent: Beeper_3/1.0 CFNetwork/485.12.7 Darwin/10.7.0
Content-Length: 89
Accept: application/json
Content-Type: application/json; charset=UTF-8
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive

Response:

{"ApplicationID":"98534bb6-82ef-4937-83e7-10f65780bf36","contact_id":43,"groups":"עבו×HTTP/1.1 400 Bad Request
Content-Length: 1647
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sun, 10 Apr 2011 07:46:09 GMT

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-    left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-    bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; }     A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active {     color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-    color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-    size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-    left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding:     5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-    wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse;     border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid;     border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td {     border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}    </style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for     more details.</p>
    </div>
  </body>
</html>

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-11-07 13:18:18

我发布的代码有错误。

NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];

此行(最初位于问题的代码中)生成了错误 - 不是采用从“[reqString UTF8String]”返回的字节数组的长度,而是给出了错误的长度 - nsstring 的长度...

修复代码:

const char *utf8String = [reqString UTF8String];    // no need to free me - autoreleased  object returned.
NSInteger l = strlen(utf8String);
NSData *requestData = [NSData dataWithBytes:utf8String length:l];

I had an error on the posted code.

NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];

This line (was originally in the question's code) generated the error - instead of taking the length of the bytes array returned from: "[reqString UTF8String]", a wrong length had been given - the length of the nsstring...

The fixed code:

const char *utf8String = [reqString UTF8String];    // no need to free me - autoreleased  object returned.
NSInteger l = strlen(utf8String);
NSData *requestData = [NSData dataWithBytes:utf8String length:l];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文