NSString *GetExRequest=[NSString stringWithFormat:
@"<?xmlversion=\"1.0\"encoding=\"utf-8\"?>\n"
"<soap:Envelopexmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetEXRatexmlns=\"Web Services\">\n"
"<AGENT_CODE>xyzzyx</AGENT_CODE>\n"
"<USER_ID>12345689</USER_ID>\n"
"<PASSWORD>dkasdja</PASSWORD>\n"
"<AGENT_SESSION_ID>xyz1234</AGENT_SESSION_ID>\n"
"<TRANSFERAMOUNT>1000</TRANSFERAMOUNT>\n"
"<PAYMENTMODE>c</PAYMENTMODE>\n"
"<CALC_BY>p</CALC_BY>\n"
"<PAYOUT_AGENT_ID>20100008</PAYOUT_AGENT_ID>\n"
"<PAYOUT_COUNTRY>Bangladesh</PAYOUT_COUNTRY>\n"
"</GetEXRate>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSLog(@"%@",GetExRequest);
NSURL *url = [NSURL URLWithString:@"https://www.prabhuusa.com/SendWsv2/txnservice.asmx?wsdl"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [GetExRequest length]];
[theRequest addValue:@"www.prabhuusa.com" forHTTPHeaderField:@"Host"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"WebServices/GetEXRate" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [GetExRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
我試圖通過發送此SOAP請求來使用此WSDL定義。我想將響應存儲到一個字符串並解析它。將SOAP響應轉換爲NSString
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [_webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [_webData mutableBytes] length:[_webData length] encoding:NSUTF8StringEncoding];
NSLog(@"WEBDATA::: %@",theXML);
}
但是響應字符串是空的。我用SOAP客戶端發送的SOAP請求檢查了這個Web服務,並收到了一個XML響應..但是我無法在客觀的C中得到響應。請找到我的解決方案
我在didRecieveResponse和didRecieveData方法中有這個
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webData appendData:data];
}
您是否曾經將'_webData'初始化爲'[NSMutableData data]'(用於'didReceiveResponse'中的檢查)?你是否在'didReceiveData'中追加數據?在'connectionDidFinishLoading'中,'_webData'是長度爲零的「NSMutableData」,還是'nil'? (順便說一句,我通常使用'initWithData'而不是'initWithBytes',因爲它看起來更直觀,但這不是你問題的根源。) – Rob
對不起,我在這裏也添加了'didRecieveResponse'和'diRecieveData'代碼。如果我搞砸了SOAP ACTION頭,我得到了XML格式的錯誤響應,所以我猜接收端是正確的,發送請求時我可能會做錯嗎? –
@YugeshShresta你還沒有展示你實例化webData的位置(如果你得到'nil'作爲響應,那是一個典型的問題)。但是,如果您收到回覆,並且其報告出現錯誤,那麼可能是您的請求。例如,我注意到你有一個開放的標籤'',我認爲應該是''。但我真的不知道是否有其他問題(請求的性質可能會有所不同,我不知道你的服務在尋找什麼)。 –
Rob