Iphone 带参数的Http Post请求
我有一个简单的asp.net web服务,它返回json格式的数据。我想发送带有参数的http post请求以获取json数据如何发送请求并获取数据? 发布请求:Iphone 带参数的Http Post请求,iphone,ios,json,http-post,httprequest,Iphone,Ios,Json,Http Post,Httprequest,我有一个简单的asp.net web服务,它返回json格式的数据。我想发送带有参数的http post请求以获取json数据如何发送请求并获取数据? 发布请求: POST /JsonWS.asmx/FirmaGetir HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: length firID=string HTTP/1.1 200 OK Content-Type
POST /JsonWS.asmx/FirmaGetir HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
firID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
回答:
POST /JsonWS.asmx/FirmaGetir HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
firID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
你在这里干什么
[[NSURLConnection alloc] initWithRequest:request delegate:self];
此行返回一个NSURLConnection,但您没有存储它。这对你毫无帮助
在读取数据之前,您正在清除数据:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
response = [[NSMutableData data] retain]; // This line is clearing your data get rid of it
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
}
编辑
-(IBAction)buttonClick:(id)sender {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.23/testService/JsonWS.asmx?op=FirmaGetir"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:15];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[@"firID=800" dataUsingEncoding:NSUTF8StringEncoding]];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];
}
#pragma NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (!self.receivedData){
self.receivedData = [NSMutableData data];
}
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
}
今天早上我遇到了这个问题,现在我才明白。我想你问题的关键是如何使用带有参数的POST方法。其实很简单 (1) 首先,您应该确保您的文件已准备好发送。这里我们说它是一个名为stringReady的NSString。我们将其用作名为postRequest的方法中的一个参数(这里不是我们要讨论的HTTP POST参数。别担心) (2)现在,我们说服务器想要获取的参数称为“数据”,这是将参数插入HTTP正文的方式。
NSString *firmadi =@"";
NSMutableData *response;
-(IBAction)buttonClick:(id)sender
{
NSString *firid = [NSString stringWithFormat:@"800"];
response = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.23/testService/JsonWS.asmx?op=FirmaGetir"]];
NSString *params = [[NSString alloc] initWithFormat:@"firID=%@",firid];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection)
{
response = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is null");
}
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)responsed
{
[response setLength:0];
NSURLResponse * httpResponse;
httpResponse = (NSURLResponse *) responsed;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
[response appendData:data];
//NSLog(@"webdata: %@", data);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
NSLog(@"error with the connection");
[connection release];
[response release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
response = [[NSMutableData data] retain];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
}
// Add the [data] parameter
NSString *bodyWithPara = [NSString stringWithFormat:@"data=%@",stringReady];
请看,这是使用POST方法时添加参数的方式。只需将参数放在要发送的文件之前。如果您知道您的参数,那么您最好查看以下网站:
这将帮助您测试是否正确发送文件,并在网站底部显示响应
(3) 第三,我们将NSString打包到NSData并将其发送到服务器
// Convert the String to NSData
NSData *postData = [bodyWithPara dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Set the content length and http body
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
[req addValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:postData];
// Create an NSURLSession
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Do something with response data here - convert to JSON, check if error exists, etc....
if (!data) {
NSLog(@"No data returned from the sever, error occured: %@", error);
return;
}
NSLog(@"got the NSData fine. here it is...\n%@\n", data);
NSLog(@"next step, deserialising");
NSError *deserr;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&deserr];
NSLog(@"so, here's the responseDict\n\n\n%@\n\n\n", responseDict);
}];
[task resume];}
希望这能帮助陷入困境的人。您是否尝试过将请求中的MIME类型设置为“application/json”而不是“application/x-www-form-urlencoded”?它来自我的web服务,为什么我要更改此设置?您发布的代码有什么问题?屏幕上有一个按钮。当我按下按钮时,我想看到我的服务结果。但是没有发生。在didFinishLoading response=[[NSMutableData]retain]中,您在使用之前下载的所有数据都将丢失,请将其放在NSString*responseString之后…感谢关注。我是ios开发新手。因此,在我的代码中有一些愚蠢的代码块。我问问题就是为了这个原因。我将尝试这些,但请编写此过程的完整代码。谢谢,我明天将尝试。我找到了不同的解决方案,但我接受您的回答,以引起您的注意。是NSURLConnection sendAsynchronousRequest:queue:completionHandler:?有几种不同的解决方案。我发布的是基于你发布的内容。它非常适合身份验证和定制。sendAsynchronousRequest:queue:completionHandler:适用于简单的异步调用。连接不需要调用start。除非使用-(id)initWithRequest:(NSURLRequest*)请求委托:(id)委托立即开始:(BOOL)startimimediately with startimimediately=NO,否则它将立即启动