首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在XML的"Net::HTTP.post_form“中找到隐藏的空白或新行?

如何在XML的"Net::HTTP.post_form“中找到隐藏的空白或新行?
EN

Stack Overflow用户
提问于 2015-04-16 12:59:20
回答 1查看 165关注 0票数 0

我使用ruby Net::HTTP.post_form将XML发布到ConnectWise服务器,使用CompanyAPI。我得到了一个错误:

代码语言:javascript
运行
复制
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

从我能找到的所有东西来看,我可能在<?xml version="1.0" encoding="utf-8"?>前有一个空格或换行符。

以下是代码:

代码语言:javascript
运行
复制
require 'net/http'
require 'uri'
require 'builder'

#f = File.new('cw.get_company.xml', 'w')
@cwhostname = 'cw_fqdn'
companyapi_url = 'https://' + @cwhostname + '/v4_6_release/apis/2.0/CompanyApi.asmx'
uri = URI(companyapi_url)

def cw_company_api_get_company
  companyid = 'company_name'
  integratorloginid = 'inegrator_login_id'
  integratorpassword = 'inegrator_passwd'
  companyidint = 0

  xml = Builder::XmlMarkup.new(:indent=>2)
  xml.instruct!
  xml.tag!('soap:Envelope'){
    xml.tag!('soap:Body'){
      xml.tag!('GetCompany xmlns="https://'+@cwhostname+'"'){
        xml.tag!('credentials'){
          xml.CompanyId(companyid)
          xml.IntegratorLoginId(integratorloginid)
          xml.IntegratorPassword(integratorpassword)
        }
      }
      xml.id(companyidint)
    }
  }
end

response = Net::HTTP.post_form(uri,[cw_company_api_get_company])
#puts cw_company_api_get_company
puts response.body

以下是带有错误的完整输出:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
   at System.Xml.XmlReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
   --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

如果我只是打印到一个文件,它似乎是好的,我如何找到空白或换行符(或任何其他可能在第1行)并删除它?

下面是在ConnectWise API文档中提供的一个示例XML:

代码语言:javascript
运行
复制
<?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>
    <GetCompany xmlns="http://connectwise.com">
      <credentials>
        <CompanyId>string</CompanyId>
        <IntegratorLoginId>string</IntegratorLoginId>
        <IntegratorPassword>string</IntegratorPassword>
      </credentials>
      <id>int</id>
    </GetCompany>
  </soap:Body>
</soap:Envelope>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-16 14:51:39

它与空格无关。您的XML是完全不正确的。问题在于字符串:

代码语言:javascript
运行
复制
xml.tag!('GetCompany xmlns="https://'+@cwhostname+'"')

显然,它将整个字符串视为一个标记,生成结束标记为:

代码语言:javascript
运行
复制
</GetCompany xmlns="https://connectwise.com">

在代码中的任何地方,puts cw_company_api_get_company都可以很容易地看到这一点。属性将传递给构建器标记,如下所示:

代码语言:javascript
运行
复制
xml.tag!('GetCompany', :xmlns => "https://#{@cwhostname}")

另一个问题是您需要将id放在GetCompany标记中。总结:

代码语言:javascript
运行
复制
def cw_company_api_get_company
  companyid = 'company_name'
  integratorloginid = 'inegrator_login_id'
  integratorpassword = 'inegrator_passwd'
  companyidint = 0

  xml = Builder::XmlMarkup.new(:indent=>2)
  xml.instruct!
  xml.tag!('soap:Envelope'){
    xml.tag!('soap:Body'){
      xml.tag!('GetCompany', :xmlns => "https://#{@cwhostname}"){
        xml.tag!('credentials'){
          xml.CompanyId(companyid)
          xml.IntegratorLoginId(integratorloginid)
          xml.IntegratorPassword(integratorpassword)
        }
        xml.id(companyidint)
      }
    }
  }
end

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29675453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档