开发者

objective-c: Webservice, How to tell about specific method to access

I am trying to access http://services-staging.cmtnyc.com/payment/payment.asmx web service and

AuthorizeCreditTrip I checked this webservice on .Net application and its working fine but in objective-c it does not responds with the same data.

The web service exposes 5 methods and I want to access 4th. But I think this code do not specify that which methood to access . forHTTPHeaderField:@"SOAPAction" tells but I do not think that its correct link to put here.

Does not responds means it sends nothing in response.

I think that I am unable to set valid SOAPAction

-(IBAction)sendData
{
    NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "​​<soap:Envelope xmlns:xsi=​\"http://www.w3.org/2001/XMLSchema-instance\" " 
                         "​​xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " 
                         "​​​​xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
                         "<soap:Header>"
                         "<AuthenticationHeader xmlns=\"http://services.cmtnyc.com/payment\">"
                         "<Username>validusername</Username>"
                         "<Password>validpass</Password>"
                         "<DataSource>validsource</DataSource>"
                         "</AuthenticationHeader>"
                         "</soap:Header>"
                         "​​​​​​<soap:Body>"
                         "<AuthorizeCreditTrip xmlns=\"http://services.cmtnyc.com/payment\">"
                         "<requestId>%d</requestId>"
                         "<deviceId>abcd</deviceId>"
                         "<userId>3003</userId>"
                         "<jobId>000047</jobId>"
                         "<paymentAmt>1</paymentAmt>"
                         "<fareAmt>0</fareAmt>"
                         "<tipAmt>0</tipAmt>"
                         "<tollAmt>0</tollAmt>"
                         "<surchargeAmt>0</surchargeAmt>"
                         "<taxAmt>0</taxAmt>"
                         "<convenienceFeeAmt>0</convenienceFeeAmt>"
                         "<swipeData></swipeData>"
                         "<accountNumber>validaccount</accountNumber>"
                         "<expiryDate>validdate</expiryDate>"
                         "<zipCode>73000</zipCode>"
                         "<cvv2>227</cvv2>"
                         "<cardReaderMethod>0</cardReaderMethod>"
                         "<encryptionKeyVersion>0</encryptionKeyVersion>"
                         "<encryptedToken></encryptedToken>"
                         "<encryptionAlgorithm>0</encryptionAlgorithm>"
                         "<pickupDate>2011-09-12</pickupDate>"
                         "<pickupLatitude>0</pickupLatitude>"
                         "<pickupLongitude>0</pickupLongitude>"
                         "<dropoffDate>2011-09-16</dropoffDate>"
                         "<dropoffLatitude>0</dropoffLatitude>"
                         "<dropoffLongitude>0</dropoffLongitude>"
                         "<passengerCount>0</passengerCount>"
                         "<tripDistance>124</tripDistance>"
                         "<tripDuration>0</tripDuration>"
                         "<readyToSettle>false</readyToSettle>"
                         "</AuthorizeCreditTrip>"
                         "</soap:Body>"
                         "</soap:Envelope>",55258];

    NSURL *url = [NSURL URLWithString: @"http://services-staging.cmtnyc.com/payment/payment.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://services.cmtnyc.com/payment/AuthorizeCreditTrip" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; //---set the HTTP method and body--- 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"%@", soapMsg); 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) 
    { 
        webData = [[NSMutableData data] retain];
    }

}

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

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

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
    [webData release]; 
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 
    NSLog(@"..DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc]    //---shows the XML---
                        initWithBytes:[webData mutableBytes] length:[webData length]
                        encoding:NSUTF8StringEncoding];
    NSLog(@"....%@",theXML);
    [theXML release];                                                                                       

    //  [activityIndicator stopAnimating];                                                                                                                                        
    [connection release];                             开发者_StackOverflow                                                                                                       
    [webData release];  
}


I've used the below code and has worked fine for me:

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://tempuri.org/SearchCustomer" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

where url is my Url, soapMsg is my soap message and @"http://tempuri.org/Method" is the soap action.


Here is how I have it working:

NSURL *urlWS = [NSURL URLWithString:kUrlServer];
NSMutableURLRequest *theRequest;
theRequest = [NSMutableURLRequest requestWithURL:urlWS];

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

Perhaps you are missing application/soap+xml.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜