我正在处理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 %@",);
}
非常感谢您的帮助。在此先感谢。
答案 0 :(得分:2)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Displaying the Datas Received %@",data);
NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Displaying the Datas Received In Readable Format %@",strResult);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
[alert show];
}
答案 1 :(得分:0)
使用git上可用的ASI HTTP库或AFHTTPClient,或者使用
下面的是发布帖子请求的代码
+(NSData *)postDataToUrl:(NSString*)urlString:(NSString*)jsonString
{
NSData* responseData = nil;
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
responseData = [NSMutableData data] ;
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSString *bodydata=[NSString stringWithFormat:@"data=%@",jsonString];
[request setHTTPMethod:@"POST"];
NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]];
[request setHTTPBody:req];
NSURLResponse* response;
NSError* error = nil;
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"the final output is:%@",responseString);
return responseData;
}
答案 2 :(得分:0)
如果您的数据太长,那么您将以块的形式接收数据,并且将自动多次调用委托didRecieveData:
。因此,您需要将响应中收到的所有数据附加到NSMutableData
。
在NSMutableData
委托中收到数据时,创建一个didRecieveData:
对象并在其中附加数据。
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myData appendData:data];
}
最后,当您的请求完成后,将该数据转换为字符串。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString * stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 3" message:@"Finished Loading" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
[alert show];
NSLog(@"Displaying the Datas Received %@", stringData);
}
答案 3 :(得分:0)
我认为你必须完成URL请求的委托方法!代码应该看起来像
@interface YourClass : UIViewController <NSURLConnectionDataDelegate> {
NSMutableData *__downloadingData;
}
//YourClass Delegation Handlers
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (!__downloadingData)
__downloadingData = [NSMutableData dataWithCapacity:0];
[__downloadingData appendData:data];
//
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert 1" message:@"Data Received" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:Nil, nil];
// [alert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Data Received %@",__downloadingData);
NSString *strResult = [[NSString alloc] initWithData:__downloadingData encoding:NSUTF8StringEncoding];
NSLog(@"Data Received In Readable Format %@",strResult);
}