既存のデータにどのようにデータを追加しますPOST NSURLRequestか?新しいパラメータを追加する必要がありますuserId=2323。
回答:
サードパーティのクラスを使用したくない場合は、次のように投稿本文を設定します...
NSURL *aUrl = [NSURL URLWithString:@"http://www.apple.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"company=Locassa&quality=AWESOME!";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
キーと値のペアを投稿文字列に追加するだけです
をNSMutableURLRequest呼び出す前に、へのすべての変更を行う必要がありますNSURLConnection。
上記のコードをコピーして貼り付けて実行するTCPMonと、この問題が発生し、リクエストがGET予期されたものではないことがわかりPOSTます。
NSURL *aUrl = [NSURL URLWithString:@"http://www.apple.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"company=Locassa&quality=AWESOME!";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
POSTリクエストの形成に関する以前の投稿はほぼ正しいです(URLではなく本文にパラメーターを追加してください)。ただし、入力データに予約文字(スペース、アンパサンド、プラス記号など)が含まれている可能性がある場合は、これらの予約文字を処理する必要があります。つまり、入力をパーセントエスケープする必要があります。
//create body of the request
NSString *userid = ...
NSString *encodedUserid = [self percentEscapeString:userid];
NSString *postString = [NSString stringWithFormat:@"userid=%@", encodedUserid];
NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:postBody];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//initialize a connection from request, any way you want to, e.g.
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
どこでprecentEscapeString次のようにメソッドが定義されます。
- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}
非常に似たようなことをするが、それを使用したいという誘惑に抵抗する有望なNSString方法stringByAddingPercentEscapesUsingEncoding(現在は非推奨)があったことに注意してください。一部の文字(スペース文字など)は処理しますが、他の一部(+または&文字など)は処理しません。
現代と同等ですstringByAddingPercentEncodingWithAllowedCharactersが、再度、使用に誘惑されていないURLQueryAllowedCharacterSetこともできますよう、+そして&エスケープ渡します。これらの2文字は、より広い「クエリ」内で許可されますが、これらの文字がクエリ内の値内に表示される場合は、エスケープする必要があります。技術的にはURLQueryAllowedCharacterSet、可変文字セットを作成してそこに含まれている文字のいくつかを削除するか、独自の文字セットを最初から作成するために使用できます。
たとえば、あなたがAlamofireのを見れば、パラメータの符号化、彼らが取るURLQueryAllowedCharacterSetと、その後削除generalDelimitersToEncode(文字を含む#、[、]、と@、しかしため、一部の古いWebサーバでの歴史的なバグの、どちら?でも/)とsubDelimitersToEncode(すなわち!、$、&、'、(、)、*、+、,、;、および=)。(あなたがの除去を議論できたが、これは正しい実装である?と/、かなり畳み込まれても、)。おそらくCFURLCreateStringByAddingPercentEscapes、より直接的/効率的です。
stringByAddingPercentEscapesUsingEncoding:は真実ですが、stringByAddingPercentEncodingWithAllowedCharacters:すべてのキャラクターで問題なく動作します。例: URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
stringByAddingPercentEncodingWithAllowedCharacters、を使用することはできません。URLQueryAllowedCharacterSetこれには+、およびが含まれているため使用できません&(これらの文字は、より広範なクエリ内で許可されている文字ですが、これらの文字がクエリ内の値内に表示される場合は、エスケープする必要があります。この文字セットでは実行されません。 )。したがって、を使用URLQueryAllowedCharacterSetして可変文字セットを作成し、そこに含まれている文字のいくつかを削除するか、独自の文字セットを最初から作成することができます。またはを使用しますCFURLCreateStringByAddingPercentEscapes。
CFURLCreateStringByAddingPercentEscapesが非推奨になったことを考えると、適切な文字セットを自分で作成する必要があります。stackoverflow.com/a/54742187/1271826を参照してください。
NSURL *url= [NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"userId=2323";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
迅速な解決策を探している人
let url = NSURL(string: "http://www.apple.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPBody = "company=Locassa&quality=AWESOME!".dataUsingEncoding(NSUTF8StringEncoding)