我正在尝试调用在PHP中创建的webservice。当我从php发送它时,请求和响应是成功的:
$.post(
'http://166.178.11.18/server/xs/reliable.php',
{"name":value2}, // any data you want to send to the script
function( data ){
$( 'body ').append( data );
});
这通过PHP代码完美地工作,但是当我试图从客观C代码发送它时,服务器无法接收参数,响应显示空值请求。 我称之为:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:currentUniqueID forKey:@"name"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Do something here
}
但是这个返回null ID值的响应。
答案 0 :(得分:2)
你可以这样做,
NSString *post =[[NSString alloc] initWithFormat:@"name=%@",currentUniqueID];
NSLog(@"post ==> %@", post);
NSURL *url=[NSURL URLWithString:@"http://166.178.11.18/server/xs/reliable.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
//NSLog(@" route id is :%@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
} else {
if (error) NSLog(@"Error: %@", error);
}
如果currentUniqueID是整数而不是@"name=%@"
将是@"name=%i"