Request with NSURLRequest

倾然丶 夕夏残阳落幕 提交于 2019-11-30 07:06:14
Mirko Lindner

try this:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL

                            URLWithString:@"http://localhost:8888/testNSURL/index.php"]

                            cachePolicy:NSURLRequestUseProtocolCachePolicy

                            timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];
NSString *postString = @"myVariable=Hello world !";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

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

if (theConnection) {

    receiveData = [NSMutableData data];   

}

seen here https://stackoverflow.com/a/6149088/1317080

Try the below code it is one of the simple ways to consume a web service(json)

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

NSMutableURLRequest *urlReq=[NSMutableURLRequest requestWithURL:url];

NSURLResponse *response;

NSError *error = nil;

 NSData *receivedData = [NSURLConnection sendSynchronousRequest:urlReq
                                              returningResponse:&response
                                                         error:&error];
if(error!=nil)
{
   NSLog(@"web service error:%@",error);
}
else
{
 if(receivedData !=nil)
 {
    NSError *Jerror = nil;

    NSDictionary* json =[NSJSONSerialization
                         JSONObjectWithData:receivedData
                         options:kNilOptions
                         error:&Jerror];

   if(Jerror!=nil)
   {
    NSLog(@"json error:%@",Jerror);
   }
 }
}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!