【问题标题】:WCF Web Service Request Error when calling with json data including danish characters使用包含丹麦字符的 json 数据调用时出现 WCF Web 服务请求错误
【发布时间】:2012-04-26 19:06:21
【问题描述】:

我的 WCF Web 服务(svc 文件)在尝试以 json 格式发送数据时返回请求错误,包括丹麦字符,如æ、ø、å。

在我的 iOS 应用中,我发出这样的请求:

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

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

我的 WCF 服务接口如下所示:

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/createNewPost")]
    NewPostReply CreateNewPost(CreatePostCredential createPostCredential);

当我不发送“æ ø å”时一切正常,但包含“æ ø å”时出现错误。

我是否必须更改运营合同或 web.config 中的某些内容?

【问题讨论】:

    标签: .net ios wcf web-services


    【解决方案1】:

    这就是我的工作:

    我不是在 NSData 中发送请求数据,而是发送一个字符串并将其设置为使用 NSUTF8StringEncoding。

    [request setHTTPBody: [requestDataString dataUsingEncoding:NSUTF8StringEncoding]]
    

    【讨论】:

    • 完美!那并删除“WrappedRequest”做到了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多