SOAP Web服务标头错误

时间:2015-04-16 13:38:01

标签: ios web-services soap

我第一次使用肥皂服务,

这是数据服务器所期望的,

POST /updateLocation.asmx HTTP/1.1
Host: www.geoming.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <setLocation xmlns="http://geoming.com/">
      <pLati>string</pLati>
      <pLongi>string</pLongi>
      <pStatusMsg>string</pStatusMsg>
      <pID>string</pID>
      <pSpeed>string</pSpeed>
      <pStreet1>string</pStreet1>
      <pStreet2>string</pStreet2>
      <pCity>string</pCity>
      <pState>string</pState>
      <pCountry>string</pCountry>
      <pDate>string</pDate>
      <pTimeZone>string</pTimeZone>
    </setLocation>
  </soap12:Body>
</soap12:Envelope>

在目标c部分,我这样做,

soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                       "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">"
                       "<soap12:Body>"
                       "<setLocation xmlns=\"http://www.geoming.com/\">"
                       "<pLati>-33.868</pLati>"
                       "<pLongi>151.2086</pLongi>"
                       "<pStatusMsg>santanu test</pStatusMsg>"
                       "<pID>3</pID>"
                       "<pSpeed>12</pSpeed>"
                       "<pStreet1>CD - 96</pStreet1>"
                       "<pStreet2>Salt lake city</pStreet2>"
                       "<pCity>kolkata</pCity>"
                       "<pState>west bengal</pState>"
                       "<pCountry>india</pCountry>"
                       "<pDate>16-04-2015</pDate>"
                       "<pTimeZone>-5.30</pTimeZone>"
                       "</setLocation>"
                       "</soap12:Body>"
                       "</soap12:Envelope>"];

        //Now create a request to the URL

        NSURL *url = [NSURL URLWithString:@"http://www.geoming.com/updateLocation.asmx"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

        //ad required headers to the request

        [theRequest addValue:@"www.geoming.com" forHTTPHeaderField:@"Host"];
        [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [theRequest addValue: @"http://www.geoming.com/setLocation" forHTTPHeaderField:@"SOAPAction"];
        [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

它始终显示&#34; System.Web.Services.Protocols.SoapException:服务器无法识别HTTP标头SOAPAction的值:http://www.geoming.com/setLocation。 System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()&#34;

由于我是ios的新手,无法找到解决方案。

2 个答案:

答案 0 :(得分:1)

错误的SOAPAction。相反@“http://www.geoming.com/setLocation”您需要使用@“http://geoming.com/setLocation

试试我的代码:

static NSString* const cstrSetLocMsg = @"<?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:Body>"
                                                    "<setLocation xmlns=\"http://geoming.com/\">"
                                                    "<pLati>-33.868</pLati>"
                                                    "<pLongi>151.2086</pLongi>"
                                                    "<pStatusMsg>santanu test</pStatusMsg>"
                                                    "<pID>3</pID>"
                                                    "<pSpeed>12</pSpeed>"
                                                    "<pStreet1>CD - 96</pStreet1>"
                                                    "<pStreet2>Salt lake city</pStreet2>"
                                                    "<pCity>kolkata</pCity>"
                                                    "<pState>west bengal</pState>"
                                                    "<pCountry>india</pCountry>"
                                                    "<pDate>16-04-2015</pDate>"
                                                    "<pTimeZone>-5.30</pTimeZone>"
                                                    "</setLocation>"
                                                    "</soap:Body>"
                                                    "</soap:Envelope>";

并创建请求:

+ (NSMutableURLRequest*) requestSetLoc{
NSString *_soapMsg = cstrSetLocMsg;
NSURL *url = [NSURL URLWithString:@"http://www.geoming.com/updateLocation.asmx"];
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc]  initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f] autorelease];
NSString *msgLength =  [NSString stringWithFormat:@"%d", [_soapMsg length]];
[req addValue:@"text/xml; charset=utf-8"  forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://geoming.com/setLocation"  forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength  forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [_soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
return req;}

它应该有效!

答案 1 :(得分:0)

将您的字符串更改为

soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                       <soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">\
                       <soap12:Body>\
                       <setLocation xmlns=\"http://www.geoming.com/\">\
                       <pLati>-33.868</pLati>\
                       <pLongi>151.2086</pLongi>\
                       <pStatusMsg>santanu test</pStatusMsg>\
                       <pID>3</pID>\
                       <pSpeed>12</pSpeed>\
                       <pStreet1>CD - 96</pStreet1>\
                       <pStreet2>Salt lake city</pStreet2>
                       <pCity>kolkata</pCity>\
                       <pState>west bengal</pState>\
                       <pCountry>india</pCountry>\
                       <pDate>16-04-2015</pDate>\
                       <pTimeZone>-5.30</pTimeZone>\
                       </setLocation>
                       </soap12:Body>\
                       </soap12:Envelope>"];