English 中文(简体)
发送utf-8时的答复500
原标题:Response 500 when sending utf-8

当我向我的网络服务发出要求时,如“Ö”,我回复500“请求处理有误” 这就是我对SOS的看法。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    NSError *error = nil;

    dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:self.responseData error:&error];
    for (id key in dict) {
        NSLog(@"%@=%@", key, [dict objectForKey:key]);
    }

If I send a message with just plain english it works fine.

I m not sure if the error is on the server-side or in the app itself. This is how I send my JSON POST:

    - (void)stringWithUrl:(NSURL *)url jsonReq:(NSString *)json{

    self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
    self.responseData = [NSData dataWithBytes:[json UTF8String] length:[json length]];


    [self.requestURL setHTTPMethod:@"POST"];
    [self.requestURL setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [self.requestURL setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [self.requestURL setValue:@"application/json" forHTTPHeaderField:@"UTF-8"];
    [self.requestURL setValue:[NSString stringWithFormat:@"%d", [self.responseData length]] forHTTPHeaderField:@"Content-Length"];
    [self.requestURL setHTTPBody: self.responseData];

这也是网络服务如何看待:

    ....    
[WebService(Namespace = "http://mypage.com/webservicesF")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX,
    [System.Web.Script.Services.ScriptService]
.....
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
最佳回答

This line:

self.responseData = [NSData dataWithBytes:[json UTF8String] length:[json length]];

是不正确的。 长度为UTF-16型,而不是UTF-8型。 你可以做这样的事情:

const char* utf8 = [json UTF8String];
self.responseData = [NSData dataWithBytes:utf8 length:strlen(utf8)];

但更好的办法是,直接创建<条码>NSData,而不采用C类示。

self.responseData = [json dataUsingEncoding:NSUTF8StringEncoding];
问题回答

a. 试图在寄出像:

    NSString *strEncode =  [src stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding];




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签