当我使用 utf-8 特殊字符(例如“Ö”)向我的 Web 服务发送请求时,它返回响应 500“处理请求时出错”这就是我在 iOS 应用程序中登录的内容。
- (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]);
}
如果我只用简单的英语发送消息,它就可以正常工作。
我不确定错误是在服务器端还是在应用程序本身。这就是我发送 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];
这就是 Web 服务的外观:
....
[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)]