如何使用NSURLConnection将数据发送到php服务器?

时间:2015-05-18 14:41:16

标签: ios objective-c post nsurlconnection

我需要一个应用程序,我需要将数据(“Hello”)发送到服务器。并得到一个回复​​说“再见”。

如果我错了,请纠正我。如果问题得到解答,我将非常感激。

NString *parameter = [NSString stringWithFormat:@"Hello=%@",_textData]; //Parameter values from user.

NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSURL *url = [NSURL URLWithString:@"http://10.0.2.208/trail/sanj.php?&data=Hello"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:parameterData];

NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(!connection1)
{
    //connected

    NSLog(@"Error Connecting.");
}

下面是PHP服务器代码:

<?php
if($_POST['data'])
{
    echo "Bye";
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

NSURL * url = [NSURL URLWithString:@“http://10.0.2.208/trail/sanj.php?data=Hello”];