12

メソッドを POST 以外に設定して変更可能なリクエストに本文を設定すると、本文がリクエストに含まれず、サーバーが応答したときに kCFErrorDomainCFNetwork エラー 303 (kCFErrorHTTPParseFailure) が発生します。メソッドを POST に変更するだけで、リクエストがエラーなしで処理されます。body を他のメソッドにアタッチする方法はありますか? それとも、すべて POST にこだわっていますか?

これは提出コードです:

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:assembleURL]];// cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:45.0];

#if (SERVER_TARGET_ARGS_ALLOWED==1)
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[req setHTTPMethod:ServerMessageMethods[operation]];    //value is @"POST" or other method name

#endif  

//run the payload into a JSON
SBJsonWriter *json = [[SBJsonWriter alloc] init];
NSString *encodedPayload = [json stringWithObject:payload];
encodedPayload = [NSString stringWithFormat:@"%@", [encodedPayload stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataPayload = [encodedPayload dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPBody:dataPayload];

NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
4

3 に答える 3

14

それに関するより最近の情報を見つけようとしましたが、あなたが話している正確な問題についての古い投稿がありました: http://lists.apple.com/archives/cocoa-dev/2004/May/msg01847.html

基本的に、彼らはそれがコードのバグであると述べています。悲しいことに、それを確認するもっと最近のものを見つけたいと思いました.

私が使用しているコードはASIHTTPRequestであり、PUT 要求を確実に実行できます。これは、低レベルのコード セットを使用して HTTP メッセージを作成し、NSMutableUrlRequest.

また、この問題と PUT リクエストに必要なものについて説明している別のブログ投稿を見つけました。 http://iphonedevelopment.blogspot.com/2008/06/http-put-and-nsmutableurlrequest.html

ブログ投稿:

NSMutableURLRequest を使用して HTTP PUT 要求を実行する場合は、次のコード行を追加します (req は NSMutableURLRequest です)。

[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

それだけです。このコード行を追加すると、PUT リクエストは正常に機能します。

于 2010-08-12T15:36:42.560 に答える
0

Content-Length/Transfer-EncodingContent-Typeヘッダーを設定することを覚えていますか? 自動的に入力されるとは思いません。Content-Lengthつまり、サーバーはそれが 0 であると想定します。

詳細: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

于 2010-08-12T15:23:56.227 に答える
-1

ここにあります

+(NSString*)simpleCurl:(NSString*)urlStr withBody:(NSString*)post{

//    NSLog(urlStr);
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
//    NSLog([url description]);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"close" forHTTPHeaderField:@"Connection"];
[req setValue:@"utf-8;q=0.7,*;q=0.7" forHTTPHeaderField:@"Accept-Charset"];
[req setValue:@"gzip,deflate" forHTTPHeaderField:@"Accept-Encoding"];
[req setValue:@"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" forHTTPHeaderField:@"User-Agent"];
[req setValue:@"Application/Json, text/html, application/xhtml+xml, */*" forHTTPHeaderField:@"Accept"];
[req setHTTPMethod:@"POST"];//  or PUT
NSString *postString = post;
[req setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSData *res = [NSURLConnection  sendSynchronousRequest:req returningResponse:NULL error:NULL];
NSString* html=[[NSString alloc] initWithData:res encoding:NSUTF8StringEncoding];
return html;

}

于 2013-05-17T21:23:50.163 に答える