0

IIS7 .NET Framework 4.0 で実行されている C# で記述された WCF Web サービスへの POST 要求を作成しようとしています。

Web サービスは GET 要求に対して機能しますが、POST メソッドが機能していないようです。.NET に切り替える前に、サーバー側に PHP を使用していたという背景があります。

iOS での私のリクエストのコード:

NSArray *jsonKeys = [NSArray arrayWithObjects:@"zip", nil];
NSArray *jsonValues = [NSArray arrayWithObjects: zipcode, nil];
NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:jsonValues forKeys:jsonKeys];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/weather", ConnectionString3]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

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

WCF Web サービスの C# コード:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(string zip);

エラーが発生したサーバーからの XML 応答を示す iOS 側の応答をログに記録し、サーバー側のログを確認しましたが、問題はないようです。どんな助けでも大歓迎です。

私が見つけることができるサーバーからのログのみが読み取ります:

(Date and Time) (Server IP) POST /PeopleService/PeopleService.svc/weather - 80 - (local app ip) AppName/1.0+CFNetwork/609+Darwin/11.4.2 400 0 0 0
4

2 に答える 2

0

問題は、操作の本体スタイルを として宣言したことですが、パラメーターが受信するデータをパラメーター名にラップBareして送信していることです。

操作を次のように宣言すると、

[OperationContract]
[WebInvoke(Method = "POST",
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.WrappedRequest,
           UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(string zip);

リクエストボディは好きなように送信できます{"zip":"30309"}

于 2013-01-10T21:55:28.697 に答える
0

JSON がマップされるようなクラスを作成することで、問題を解決できました。

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(ZipCodeJSON zipJson);

[DataContract]
public class ZipCodeJSON
{
    [DataMember]
    public string zip { get; set; }
}
于 2013-01-10T21:09:55.703 に答える