iOS POST請求 有中文的時候,iospost

來源:互聯網
上載者:User

iOS POST請求 有中文的時候,iospost

鑌哥,最近post請求遇到一些問題,當有中文的時候是請求不成功

下面鑌哥寫兩種案例:

1:通常post請求:(有中文不成功)

//1:設定URL

    NSString *host = HOST;

    NSString *usename = @"fuck";

    NSString *queryString = [NSStringstringWithFormat:@"/app/clickTableScreen?userName=%@&idfa=%@&operator=%@&systemVersion=%@&networkState=%@&deviceName=%@&memorySize=%@&model=%@",usename,adId, [self checkCarrier],phoneVersion,netStr,userPhoneName,totalDiskSpaceStr, [LoginViewControllerdeviceString]] ;

     NSLog(@"querString:%@",queryString);

   //完整的設定參數

    NSString *urlString = [NSStringstringWithFormat:@"%@%@",host,queryString];

    NSLog(@"參數:%@",urlString);

    //得到完整的url

    NSURL *url1 = [NSURLURLWithString:urlString];



    //2:Request請求

            NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    //第三步,串連伺服器

    NSError * error = nil;

    NSData *reqData  = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:&error];

    

//    //3:將請求轉換二進位

//    NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if(reqData == nil){

        if(error){//這樣寫有個好處,就是如果解析資料不成功,系統會列出原因,我就因為寫了這句話才知道出現什麼原因:(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x)

             NSLog(@"error = %@", error);

        }

        UIAlertView * alert = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"網路不穩定,請稍後嘗試!"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"ok",nil];

        [alert show];

        return;

    }else{

        NSLog(@"error = %@", error);

    }

    NSDictionary *reqDic=[NSJSONSerializationJSONObjectWithData:reqData options:NSJSONReadingAllowFragmentserror:nil];

    NSString *info = [NSStringstringWithFormat:@"%@",[reqDicobjectForKey:@"info"]];

    NSLog(@"參數字典:%@",reqDic);

    //4:列印資料

    NSString *errcode = [NSStringstringWithFormat:@"%@", [reqDicobjectForKey:@"errcode"]];

    if ([errcode isEqualToString:@"0"]) {

        NSLog(@"成功了");

    }else

    {

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"擷取廣告標識符失敗"message:info delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];

        [alert show];

    }

}



2:第二種寫法對比(其實就是加了一句話)

/1:設定URL

    NSString *host = HOST;

    NSString *usename = @"fuck";

    NSString *queryString = [NSString stringWithFormat:@"/app/clickTableScreen?userName=%@&idfa=%@&operator=%@&systemVersion=%@&networkState=%@&deviceName=%@&memorySize=%@&model=%@",usename,adId, [self checkCarrier],phoneVersion,netStr,userPhoneName,totalDiskSpaceStr, [LoginViewController deviceString]] ;

     NSLog(@"querString:%@",queryString);

   //完整的設定參數

    NSString *urlString = [NSString stringWithFormat:@"%@%@",host,queryString];

    NSLog(@"參數:%@",urlString);

    //得到完整的url

    //有中文的時候要轉碼

    NSString * urlstr = [urlStringstringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSURL * url = [NSURLURLWithString:urlstr];



    //2:Request請求

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    //第三步,串連伺服器

    NSError * error = nil;

    NSData *reqData  = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    

//    //3:將請求轉換二進位

//    NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if(reqData == nil){

        if(error){//這樣寫有個好處,就是如果解析資料不成功,系統會列出原因,我就因為寫了這句話才知道出現什麼原因:(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x)

             NSLog(@"error = %@", error);

        }

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"網路不穩定,請稍後嘗試!"delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"ok", nil];

        [alert show];

        return;

    }else{

        NSLog(@"error = %@", error);

    }

    NSDictionary *reqDic=[NSJSONSerialization JSONObjectWithData:reqData options:NSJSONReadingAllowFragments error:nil];

    NSString *info = [NSString stringWithFormat:@"%@",[reqDic objectForKey:@"info"]];

    NSLog(@"參數字典:%@",reqDic);

    //4:列印資料

    NSString *errcode = [NSString stringWithFormat:@"%@", [reqDic objectForKey:@"errcode"]];

    if ([errcode isEqualToString:@"0"]) {

        NSLog(@"成功了");

    }else

    {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"擷取廣告標識符失敗" message:info delegate:selfcancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

        [alert show];

    }

}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.