Проблема с кодировкой URL? - PullRequest
0 голосов
/ 18 июня 2010

здесь я использую кодировку URL, используя следующие методы:

Я передаю URL в качестве параметра этому методу

-(NSData *)addActon:(CustomerDetails*)aCustomerDetails
{
NSString* addCustomerString=@"";
addCustomerString=[addCustomerString stringByAppendingString:@"<Customer>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<Name>"];
if([aCustomerDetails.customerName length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerName];

}
addCustomerString=[addCustomerString stringByAppendingString:@"</Name>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<PaymentsDue>"];
if([aCustomerDetails.customerPaymentsDue length]!=0)
{
    if([aCustomerDetails.customerPaymentsDue isEqualToString:@"Due On Receipt"])
    {
        addCustomerString=[addCustomerString stringByAppendingString:@"0"];
    }
    else
    {
        NSString *str1=[aCustomerDetails.customerPaymentsDue stringByReplacingOccurrencesOfString:@"Net" withString:@""];
        str1=[str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
        addCustomerString=[addCustomerString stringByAppendingString:str1];

    }

}
addCustomerString=[addCustomerString stringByAppendingString:@"</PaymentsDue>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CurrencyCode>"];
if([aCustomerDetails.customerCurrencyCode length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerCurrencyCode];

}
addCustomerString=[addCustomerString stringByAppendingString:@"</CurrencyCode>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingAddress>"];
if([aCustomerDetails.customerBillingAddress length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingAddress];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingAddress>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingCity>"];
if([aCustomerDetails.customerBillingCity length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingCity];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingCity>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingState>"];
if([aCustomerDetails.customerBillingState length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingState];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingState>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingZip>"];
if([aCustomerDetails.customerBillingZip length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingZip];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingZip>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingCountry>"];
if([aCustomerDetails.customerBillingCountry length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingCountry];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingCountry>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<BillingFax>"];
if([aCustomerDetails.customerBillingFax length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerBillingFax];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</BillingFax>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingAddress>"];
if([aCustomerDetails.customerShippingAddress length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerShippingAddress];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingAddress>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingCity>"];
if([aCustomerDetails.customerShippingCity length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerShippingCity];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingCity>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingState>"];
if([aCustomerDetails.customerShippingState length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerShippingState];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingState>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingZip>"];
if([aCustomerDetails.customerShippingZip length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString: aCustomerDetails.customerShippingZip];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingZip>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingCountry>"];
if([aCustomerDetails.customerShippingCountry length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerShippingCountry];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingCountry>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<ShippingFax>"];
if([aCustomerDetails.customerShippingFax length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customerShippingFax];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</ShippingFax>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<Contacts>"];
printf("\n isnde add action for add customer contacts list count :%d",[aCustomerDetails.contactList count]);

for(int i=0;i<[aCustomerDetails.contactList count];i++)
{
    Contacts* object=[aCustomerDetails.contactList objectAtIndex:i];
    printf("\n insde add customers conatct id :%s",[object.customerContactId UTF8String]);
    addCustomerString=[addCustomerString stringByAppendingString:@"<Contact>"];

    addCustomerString=[addCustomerString stringByAppendingString:@"<Salutation>"];
    if([object.customerSalutation length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerSalutation];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</Salutation>"];
    addCustomerString=[addCustomerString stringByAppendingString:@"<FirstName>"];
    if([object.customerFirstName length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerFirstName];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</FirstName>"];
    addCustomerString=[addCustomerString stringByAppendingString:@"<LastName>"];
    if([object.customerLastName length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerLastName];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</LastName>"];
    addCustomerString=[addCustomerString stringByAppendingString:@"<EMail>"];
    if([object.customerEmail length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerEmail];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</EMail>"];
    addCustomerString=[addCustomerString stringByAppendingString:@"<Phone>"];
    if([object.customerPhone length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerPhone];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</Phone>"];
    addCustomerString=[addCustomerString stringByAppendingString:@"<Mobile>"];
    if([object.customerMobile length]!=0)
    {
        addCustomerString=[addCustomerString stringByAppendingString:object.customerMobile];
    }
    addCustomerString=[addCustomerString stringByAppendingString:@"</Mobile>"];

    addCustomerString=[addCustomerString stringByAppendingString:@"</Contact>"];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</Contacts>"];

addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFields>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldLabel1>"];
if([aCustomerDetails.customFieldLabel1 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldLabel1];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldLabel1>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldValue1>"];
if([aCustomerDetails.customFieldValue1 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldValue1];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldValue1>"];

addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldLabel2>"];
if([aCustomerDetails.customFieldLabel2 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldLabel2];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldLabel2>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldValue2>"];
if([aCustomerDetails.customFieldValue2 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldValue2];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldValue2>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldLabel3>"];
if([aCustomerDetails.customFieldLabel3 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldLabel3];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldLabel3>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<CustomFieldValue3>"];
if([aCustomerDetails.customFieldValue3 length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.customFieldValue3];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFieldValue3>"];
addCustomerString=[addCustomerString stringByAppendingString:@"</CustomFields>"];
addCustomerString=[addCustomerString stringByAppendingString:@"<Notes>"];
if([aCustomerDetails.notes length]!=0)
{
    addCustomerString=[addCustomerString stringByAppendingString:aCustomerDetails.notes];
}
addCustomerString=[addCustomerString stringByAppendingString:@"</Notes>"];
addCustomerString=[addCustomerString stringByAppendingString:@"</Customer>"];

printf("\n ================addCustomerString %s",[addCustomerString UTF8String]);
ZohoAppDelegate *appDelegate =  (ZohoAppDelegate*) [[UIApplication sharedApplication] delegate];
NSString* stringB = [self _encodeString:addCustomerString];
NSString* curl = @"https://invoice.zoho.com/api/customers/create?ticket=";
curl = [curl stringByAppendingString:appDelegate.ticket];
curl = [curl stringByAppendingString:@"&apikey=bfc9c6dde7c889a19f8deea9d75345cd&XMLString="];
curl = [curl stringByAppendingString:stringB];
curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSURL *finalURL = [NSURL URLWithString:curl];

NSData *returnData;

[self updateStatus];
if (internetConnectionStatus == NotReachable) 
{
    reachbleAlert = [[UIAlertView alloc] initWithTitle:@"Zoho Invoice"
                                               message:@"No Network Available. \n This Application requires network connectivity. "
                                              delegate:self 
                                     cancelButtonTitle:@"Ok"
                                     otherButtonTitles: nil];
    [reachbleAlert show];   
    [reachbleAlert release];

}
else 
{
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL 
                                                              cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                          timeoutInterval:10];
    [theRequest setHTTPMethod:@"POST"];
    returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    printf("\n return string:%s",[returnString UTF8String]);
}



return returnData;

}

 - (NSString *)_encodeString:(NSString *)string
{
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, 
                 (CFStringRef)string, 
                 NULL, 
                 (CFStringRef)@";/?:@&=$+{}<>,",
                 kCFStringEncodingUTF8);
    return [result autorelease];
}

но я передаю параметр как имя клиента: 1234 ~! @ # $% ^ & * (((((()) _ + | "?:> <.;; метод кодирует клиента вместе с URL-адресоми я получаю сообщение об ошибке, так как URL не очень хорошо сформирован. </p>

Может кто-нибудь подсказать, как из этого выйти ...

Любая помощь будет высоко ценится. Спасибо, Мониш.

1 Ответ

0 голосов
/ 18 июня 2010

Как правило, вам не следует добавлять данные о клиентах в ваш URL. Запрос является запросом Http POST, и отправляемые данные отправляются с помощью метода setHTTPBody: см. NSMutableURLRequest . Таким образом, вам не нужно помещать его в ту же строку, что и URL. URL - это адрес запроса, а не его данные

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...