第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用NSURLSession發(fā)送POST請求

使用NSURLSession發(fā)送POST請求

iOS
素胚勾勒不出你 2019-11-04 15:46:57
我正在嘗試使用對遠程REST API執(zhí)行POST請求NSURLSession。這個想法是使用兩個參數(shù)發(fā)出請求:deviceId和textContent。問題是服務器無法識別這些參數(shù)。服務器部分正常工作,因為我已經(jīng)使用POSTMAN發(fā)送了適用于Google Chrome的POST,并且工作正常。這是我現(xiàn)在正在使用的代碼:NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];NSString *textContent = @"New note";NSString *noteDataString = [NSString stringWithFormat:@"deviceId=%@&textContent=%@", deviceID, textContent];NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];sessionConfiguration.HTTPAdditionalHeaders = @{                                               @"api-key"       : @"API_KEY",                                               @"Content-Type"  : @"application/json"                                               };NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];NSURL *url = [NSURL URLWithString:@"http://url_to_manage_post_requests"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];request.HTTPMethod = @"POST";NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {    // The server answers with an error because it doesn't receive the params}];[postDataTask resume];我已經(jīng)嘗試了相同的過程NSURLSessionUploadTask:// ...NSURL *url = [NSURL URLWithString:@"http://url_to_manage_post_requests"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];request.HTTPMethod = @"POST";NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:[noteDataString dataUsingEncoding:NSUTF8StringEncoding] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {    // The server answers with an error because it doesn't receive the params}];[uploadTask resume];有任何想法嗎?
查看完整描述

3 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

我的方法的問題在于,我在發(fā)送Content-Type所有請求時都發(fā)送了不正確的標頭。因此,代碼正常運行所需的唯一更改是刪除Content-Type = application/jsonHTTP標頭。因此正確的代碼是這樣的:


NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];

NSString *textContent = @"New note";

NSString *noteDataString = [NSString stringWithFormat:@"deviceId=%@&textContent=%@", deviceID, textContent];


NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

sessionConfiguration.HTTPAdditionalHeaders = @{

                                               @"api-key"       : @"API_KEY"

                                               };

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

NSURL *url = [NSURL URLWithString:@"http://url_to_manage_post_requests"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];

request.HTTPMethod = @"POST";

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    // The server answers with an error because it doesn't receive the params

}];

[postDataTask resume];

發(fā)送圖像以及其他參數(shù)

如果您需要使用NSURLSession此處發(fā)布圖像以及其他參數(shù),請舉一個例子:


NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceID"];

NSString *textContent = @"This is a new note";


// Build the request body

NSString *boundary = @"SportuondoFormBoundary";

NSMutableData *body = [NSMutableData data];

// Body part for "deviceId" parameter. This is a string.

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"deviceId"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"%@\r\n", deviceID] dataUsingEncoding:NSUTF8StringEncoding]];

// Body part for "textContent" parameter. This is a string.

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"textContent"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"%@\r\n", textContent] dataUsingEncoding:NSUTF8StringEncoding]];

// Body part for the attachament. This is an image.

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"ranking"], 0.6);

if (imageData) {

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"image"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:imageData];

    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


// Setup the session

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

sessionConfiguration.HTTPAdditionalHeaders = @{

                                               @"api-key"       : @"55e76dc4bbae25b066cb",

                                               @"Accept"        : @"application/json",

                                               @"Content-Type"  : [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]

                                               };


// Create the session

// We can use the delegate to track upload progress

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];


// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background

NSURL *url = [NSURL URLWithString:@"URL_TO_UPLOAD_TO"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

request.HTTPMethod = @"POST";

request.HTTPBody = body;

NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    // Process the response

}];

[uploadTask resume];


查看完整回答
反對 回復 2019-11-04
?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

您可以嘗試使用NSDictionary作為參數(shù)。以下內(nèi)容會將參數(shù)正確發(fā)送到JSON服務器。


NSError *error;


NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                   timeoutInterval:60.0];


[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];


[request setHTTPMethod:@"POST"];

NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",

                     @"IOS TYPE", @"typemap",

                     nil];

NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];

[request setHTTPBody:postData];



NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


}];


[postDataTask resume];

希望這會有所幫助(我正在嘗試使用上述方法對CSRF真實性問題進行分類-但它確實在NSDictionary中發(fā)送了參數(shù))。


查看完整回答
反對 回復 2019-11-04
  • 3 回答
  • 0 關(guān)注
  • 985 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號