我在以SOAP格式請求服務器時遇到問題。當我在瀏覽器中粘貼肥皂信封(僅用於測試Web服務)時,它會給出正確的響應。但是當我從應用程序請求時,它給了我無關緊要的迴應。 我搜索了很多,但每一個地方,我發現肥皂請求相同的技術。 這是我的代碼,我正在使用。對ios服務器的SOAP請求
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<ProfileLogin xmlns=\"http://tempuri.org/\">"
"<strLoginRequest>"
"<email>[email protected]</email>"
"<password>123456</password>"
"</strLoginRequest>"
" </ProfileLogin>"
"</soap:Body>"
"</soap:Envelope>"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://www.abc.com.au/mobileapp.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"http://tempuri.org/ProfileLogin" forHTTPHeaderField:@"SOAPAction"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
和在根級獲取響應
<ProfileReply> <成功>假< /成功> <錯誤>數據是無效的。 1號線,位置1 < /錯誤> </ProfileReply >
請一些好友讓我知道這是我的mycode的或者從服務器端故障。
@阿寶我試過,但它給我的答覆。服務器無法爲請求提供服務,因爲媒體類型不受支持。 –
@aloksrivastava也許你會數msgLength是錯誤的。在[soapMessage dataUsingEncoding:NSUTF8StringEncoding]中返回NSData的長度;現在它給予 – larva
,值不能爲空。 –