【发布时间】:2013-11-06 06:19:11
【问题描述】:
我正在处理 HTTP GET 和 POST 请求/响应。 它与 POST 一起工作正常,但坚持使用 GET 方法..
在我的控制台中,我得到一个 379 字节的数据。但是,我需要在我的控制台中显示整个数据(机器可读格式)。之后,我会将其转换为 JSON 格式。
注释行是 POST 方法。
这是我的代码..
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username", @"password"];
// NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
[request setHTTPMethod:@"GET"];
// [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type" ];
// [request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection Not Succeeded");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
[alert show];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 2" message:@"Error Occurred" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
[alert show];
NSLog(@"Error Description is here: %@", error.description);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
[alert show];
// NSLog(@"Displaying the Datas Received %@",);
}
我们非常感谢您的帮助。提前致谢。
【问题讨论】:
-
GET 请求有大小限制。为什么要使用 GET 而不是 POST ?
-
要获得回复..我需要在我的控制台中查看这些数据
-
POST 请求也有响应。对 HTTP 请求有什么误解?
-
在 GET 方法中,我们会得到响应,对吧?我需要在我的控制台中显示这些响应......明白了吗?
-
将此行
NSLog(@"Displaying the Datas Received %@",data);放入您的-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data委托方法中
标签: ios iphone objective-c get httprequest