0

我正在处理 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 %@",);

}

非常感谢您的帮助。提前致谢。

4

4 回答 4

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];

}
于 2013-11-06T07:00:49.137 回答
0

使用 git 上可用的 ASI HTTP 库或 AFHTTPClient 或使用可以使用

下面是发出 post 请求的代码

+(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;
}
于 2013-11-06T06:38:41.273 回答
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);

}
于 2013-11-06T08:14:46.030 回答
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);


}
于 2013-11-06T08:22:28.140 回答