11
我想打電話給在Objetive C織物服務,返回的XML數據,我要在包頭傳遞一些信息到服務器, 就像在JavaScript中,我們可以使用jQuery做,如何在Objective-C中設置Http頭字段?
x.setRequestHeader('鍵','值');
其中x是xmlHttpRequest對象。 我們如何傳遞NSConnection類中的標題數據, 我用谷歌,但沒有找到很好的解決方案。 請幫助我。
我想打電話給在Objetive C織物服務,返回的XML數據,我要在包頭傳遞一些信息到服務器, 就像在JavaScript中,我們可以使用jQuery做,如何在Objective-C中設置Http頭字段?
x.setRequestHeader('鍵','值');
其中x是xmlHttpRequest對象。 我們如何傳遞NSConnection類中的標題數據, 我用谷歌,但沒有找到很好的解決方案。 請幫助我。
您可以使用NSMutableURLRequest類在頭中傳遞信息,然後調用NSURLConnection類(它將調用連接委託)。
請看下面的代碼,在NSMutableURLRequest
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
//do post request for parameter passing
[theRequest setHTTPMethod:@"POST"];
//set the content type to JSON
[theRequest setValue:@"xml" forHTTPHeaderField:@"Content-Type"];
//passing key as a http header request
[theRequest addValue:@"value1" forHTTPHeaderField:@"key1"];
//passing key as a http header request
[theRequest addValue:@"value2" forHTTPHeaderField:@"key2"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
[theConnection release];
添加俗鍵 - 值不適用於HTTPS URL中...任何想法工作嗎? – DShah 2013-09-16 12:43:51
網址是HTTP或HTTPS無關緊要。無論哪種情況,NSMutableRequest都會發送屬性。您是否處理HTTPS服務的身份驗證委託? – 2013-09-17 11:43:08
您聲稱將內容類型設置爲JSON,但尚未將其設置爲xml?你必須將它設置爲'application/json' – Houman 2014-09-22 11:57:06