0

我试图将一些数据发布到 php 页面并且发布的元素没有通过。我知道这是因为 php 返回$_REQUEST,这只是 phpsession,而不是应该发布的用户名。什么不在这里?

NSString *urlString = @"http://www.example.com/api";


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"thisisit\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];



NSURLResponse *response = [[NSURLResponse alloc] init];
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
4

1 回答 1

0

您需要设置最终边界。在之前添加此代码[request setHTTPBody:body]

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
于 2012-10-18T16:53:09.060 回答