简体   繁体   English

使用NSURLRequest将数据POST到服务器的问题

[英]Problem using NSURLRequest to POST data to server

I create an NSURLRequest to post my data in the iPhone application to a server to proceed the PHP script. 我创建了一个NSURLRequest ,将我在iPhone应用程序中的数据发布到服务器上以继续PHP脚本。 My PHP script is look like this. 我的PHP脚本看起来像这样。

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];

    $link = mysql_connect("localhost", "fffasfdas","Nfdsafafs") or die ("Unable to connect to database.");
    mysql_select_db("muradsbi_mydatabase") or die ("Unable to select database.");

    $sqlstatement= "INSERT INTO dbname (name,email) VALUES ('$name','$email')";
    $newquery = mysql_query($sqlstatement, $link);
    echo 'thanks for your register';
?>

and my NSURLRequst is created like below. 我的NSURLRequst创建如下。

NSString *myRequestString = @"&name=Hello%20World&email=Ohai2u";
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.google.com/"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

However, this site is unable to get the data from this application and save it to the database, but I know it was connected succussfully because my application is able to get the response data from the server. 但是,此站点无法从此应用程序获取数据并将其保存到数据库,但我知道它连接成功,因为我的应用程序能够从服务器获取响应数据。 I don't know whether my variable name is declared in the wrong way or others issues. 我不知道我的变量名是以错误的方式声明还是其他问题。 How can I fix it? 我该如何解决?

You should remove the leading & in myRequestString and the problem is likely that the correct content-type header is not being sent. 您应该删除前导& in myRequestString ,问题可能是没有发送正确的content-type标头。 Try adding a call to 尝试添加呼叫

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

You should also not pass nil for error, so you can see what the client thinks is going on. 您也不应该为错误传递nil ,因此您可以看到客户端认为正在发生的事情。

Unrelated, but your PHP code is open to SQL injection attacks . 不相关,但您的PHP代码对SQL注入攻击是开放的。

It should be noted that ... 应该注意的是 ...

NSData *data = [NSData dataWithBytes: [myRequestString UTF8String]
                              length: [myRequestString length]];

... is very bad and can cause vague errors. ......非常糟糕,可能会导致模糊的错误。 With UTF8 there is zero guarantee that the number of bytes in your encoded string is the same as the number of characters in your string. 对于UTF8,无法保证编码字符串中的字节数与字符串中的字符数相同。

So instead, you should simply use: 所以相反,你应该简单地使用:

NSData *data = [myRequestString dataUsingEncoding: NSUTF8StringEncoding];

try this 试试这个

NSString *name = [[NSString alloc]initWith String: @"Hello World"];     
NSString *email = [[NSString alloc]initWith String: @"Ohai2u"];     
NSString *urlString = [NSString stringWithFormat:@"http://somedomain.com/sendMail.php?name=%@&email=%@", 
                           [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [[NSURL alloc] initWithString:urlString];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    NSData *urlData;
    NSURLResponse *response;
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
    [url release];

just dont forget to release the strings 只是不要忘记释放字符串

set the headers in the PHP script 在PHP脚本中设置标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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