【问题标题】:SOAP webservice header errorSOAP Web 服务标头错误
【发布时间】:2015-04-16 13:38:00
【问题描述】:

我是第一次使用肥皂服务,

这是数据服务器期望的,

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]];

总是显示“System.Web.Services.Protocols.SoapException:服务器无法识别 HTTP Header SOAPAction 的值:http://www.geoming.com/setLocation.System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()”。

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

【问题讨论】:

    标签: ios web-services soap


    【解决方案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;}
    

    应该可以了!

    【讨论】:

    • 嗨,谢谢 .. 它显示返回错误。你能帮忙吗,错误是“字符串或二进制数据将被截断”
    • 我觉得有些字段对服务器有限制。例如,如果某个字段是 varchar(10),并且您尝试在其中输入 20 个字符,则会收到此错误。
    【解决方案2】:

    把你的字符串改成

    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>"];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多