簡體   English   中英

iOS-使用特定的MIME類型發出HTTP請求

[英]IOS - Make http request with specific mime type

我在本地apache服務器上運行REST服務。 我試圖發送http請求以從ios應用程序觸發它。 我已經嘗試了多種方式,但是請求似乎沒有到達服務器。

編輯:REST服務根據特定的MIME類型決定要調用的內容,這就是為什么我要設置內容類型的原因。

這是我目前所擁有的代碼:

- (IBAction)fetchTweet
{
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request addValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request     delegate:self];

    self.connection = connection;
    [connection start];

}


- (IBAction)go:(id)sender
{
    [self fetchTweet];
    self.label.text = @"Working";
}

這是一個同步請求。 嘗試與此服務器同步使用以下代碼。 至少這可以確保一切正常。 可以將responseData寫入文件中以查看響應。

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];

NSURLResponse *urlResponse = nil;
NSData *responseData = nil;

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request addValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&theError];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM