-1

我正在从 iphone 应用程序将数据发布到服务器,但是在读取有关“过度不良访问”的帖子行代码时出现异常。我用于发送四个变量数据的代码相同,然后如果我在帖子中添加更多变量,它会正常工作,它会给出错误.

    NSString*category=titleCategory;
NSString*sub_Category=titleSubCategory;
NSString*content_Type=@"Audio";

content_Title=TitleTextField.text;
NSString*content_Title=content_Title;
NSString*publisher=@"Celeritas";
    content_Description=descriptionTextField.text;
NSString*content_Description=content_Description;

NSString*content_ID=@"10";
NSString*content_Source=@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav";


NSString *post =[[NSString alloc] initWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source];


NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.php"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);

下面是它破坏代码的行

    NSString *post =[[NSString alloc] initWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source];
4

3 回答 3

1
Try this


    NSString *args = @"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@";

    NSString *values=[NSString stringWithFormat:args,category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source];


    NSData *postData = [values dataUsingEncoding:NSASCIIStringEncoding                            allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSString *path = [[NSString alloc] initWithFormat:@"%s",your urlpath];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:path]];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:[values dataUsingEncoding:NSISOLatin1StringEncoding]];
    [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *err;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                                                 returningResponse:&response error:&err];

    NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    [NSURLConnection connectionWithRequest:request delegate:self];

    //NSLog(@"String==> %@",returnString);

希望这可以帮助...

祝你好运 !!

于 2013-03-20T08:15:15.013 回答
0

在我测试项目以及在 .h 文件中时,以下代码很有帮助

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController{
    NSURLConnection *connection;
    NSMutableData *responseData;
}

@end

.m 文件

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    NSString*category=@"Cat1";
    NSString*sub_Category=@"Title";
    NSString*content_Type=@"Audio";

    NSString* content_Title=@"Test";

    NSString*publisher=@"Celeritas";

    NSString*content_Description=@"Content Description";

    NSString*content_ID=@"10";
    NSString*content_Source=@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav";


    NSString*post = [NSString stringWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source];

    NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.php"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];


    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];



    connection=[NSURLConnection connectionWithRequest:request delegate:self];
    if(connection){
        responseData=[NSMutableData data];
    }
}

#pragma NSUrlConnection Delegate Methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength: 0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//    [delegate APIResponseArrived:NULL];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString =[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//    [delegate APIResponseArrived:responseString ];
    NSLog(@"%@",responseString);
}

这段代码解决了你的问题。

于 2013-03-20T09:19:39.320 回答
0

我得到了问题的解决方案,实际上他们的无效地址存在变量分配问题,这就是为什么它给出访问错误的原因我直接分配了值然后它对我来说很好

  NSString*category=@"Category";
NSString*sub_Category=@"Working";
NSString*content_Type=@"Audio";

NSString*content_Title=@"Content Title";
NSString*publisher=@"Celeritas";
NSString*content_Description=@"ContentDescription";




NSString*content_ID=@"10";
NSString*content_Source=@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav";


NSString *post =[[NSString alloc] init];


post = [NSString stringWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source];


NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.php"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
于 2013-03-20T08:22:35.167 回答