简体   繁体   English

iOS-使用特定的MIME类型发出HTTP请求

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

I have a REST service running on a local apache server. 我在本地apache服务器上运行REST服务。 I am trying to send a http request to trigger it from an ios application. 我试图发送http请求以从ios应用程序触发它。 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. 编辑:REST服务根据特定的MIME类型决定要调用的内容,这就是为什么我要设置内容类型的原因。

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. 可以将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