我正在尝试将数据发布到PHP Web服务 我很熟悉使用查询$ .post在html中这样做,但我很难在目标C中尝试这个。 我试过几个博客&在stackoverflow上找到的问题。
我终于想出了以下代码:
NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname];
NSLog(@"Request: %@", jsonRequest);
NSURL *url = [NSURL URLWithString:@"http:myurl..."];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
我也尝试过:
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
我的网络服务在帖子上创建新用户并返回用户ID。
两者都成功进行Web服务调用(创建了新的用户ID),但是他们不发布数据,即每次都创建一个空白用户。
请告诉我我是否遗漏了什么。
感谢。
我的其他尝试:
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:@"myUrl.. "]];
[request setHTTPMethod:@"POST"];
NSString *postString = @"Email=me@test.com&FirstName=Test";
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc]
initWithRequest:request delegate:self];
我终于解决了这个问题: 最后,弄清楚出了什么问题。我使用http://mypath?params=123作为我的网址。我没有完整的网址(从不需要其他语言)。但在这里我需要给htt://mypath/index.php?params = 123
答案 0 :(得分:40)
我认为你最好像这样使用NSJSONSerialization类:
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
email, @"Email",
fname, @"FirstName",
nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
[request setHTTPBody:postData];
使用字典然后将其转换为JSON比创建字符串更容易。
祝你好运
let tmp = ["email": email,
"FirstName": fname]
let postData = try? JSONSerialization.data(withJSONObject: tmp, options: .prettyPrinted)
request.httpBody = postData
答案 1 :(得分:3)
我已运行您的代码,服务器端收到所有消息。
POST / HTTP/1.1
Host: linux-test
User-Agent: demoTest/1.0 CFNetwork/548.1.4 Darwin/11.3.0
Content-Length: 44
Accept: application/json
Content-Type: application/json
Accept-Language: en
Accept-Encoding: gzip, deflate
Connection: keep-alive
{"Email":"test@test.com","FirstName":"test"}
按照代码,如果jsonRequest
包含非ASCII字符,则[jsonRequest length]
将出错。
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
您可以改为使用strlen
。
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:strlen([jsonRequest UTF8String])];
或
[jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
答案 2 :(得分:0)
试试这个
NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
答案 3 :(得分:0)
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@posts", SeverURL]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"content-type" value:@"application/json"];
[request addRequestHeader:@"User-Agent" value:@"iOS"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
request.delegate=self;
NSString *json=[[self prepareData] JSONRepresentation];//SBJSON lib, convert a dict to json string
[request appendPostData:[NSMutableData dataWithData:[json dataUsingEncoding:NSUTF8StringEncoding]]];
[request startSynchronous];