0

iPhone アプリケーションが WCF サービスと通信できるようにしようとしています。私のアプリケーションは Web サービスに正常に接続/検索しますが、エラー以外の応答が返されないようです。Visual Studios wcf tester を使用すると、正常に動作します。

XML 形式:<mAccess><user></user><pwd></pwd></mAccess>

頭/体の構造に何か問題がありますか?

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:website]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];

NSString *soapMessage = [[NSString alloc] initWithFormat:@"<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body>
<MobileAccess xmlns=\"http://tempuri.org/\"><mAccess><user>user</user><pwd>pwd</pwd><custID>0</custID></mAccess>
</MobileAccess></SOAP-ENV:Body></SOAP-ENV:Envelope>"];


[request setValue:[NSString stringWithFormat:@"%d",[soapMessage length]] forHTTPHeaderField:@"Content-length"];
[request addValue:@"http://tempuri.org/IProviderDataService/MobileAccess" forHTTPHeaderField:@"Soapaction"];

[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
4

1 に答える 1

1

URLリクエストの作成には、潜在的なエラーが1つあります。「Content-Length」をHTTPボディの長さに設定する必要があります。[soapMessage length]これは、メッセージ内の非ASCII文字がUTF-8に変換される場合とは異なる場合があります。

したがって、変換後のデータの長さを使用する必要があります。

NSData *bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:@"%d",[bodyData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:bodyData];
于 2012-10-22T21:08:06.913 に答える