简体   繁体   中英

IOS - Make http request with specific mime type

I have a REST service running on a local apache server. I am trying to send a http request to trigger it from an ios application. I have tried in numerous ways but the request doesn't seem to reach the server.

EDIT: The REST service decides what to call based on a specific MIME type, that's why I'm setting the content-type.

This is what I have at the moment for code:

- (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";
}

This is a synchronous request. Try using the below code to this the server synchronously. At least this will make sure everything is running fine. responseData can be written in a file to see the response.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM