具有基本身份验证的Groovy httpBuilder POST XML

时间:2015-02-05 23:56:52

标签: xml post groovy httpbuilder

我正在尝试向Restful WS发送POST请求,请求最初为xml,响应也是如此。

我还需要发送基本身份验证。 起初我遇到的问题是没有定义类,谢天谢地,需要6个罐来解决这个问题。

现在我继续得到以下内容: 抓到:groovyx.net.http.HttpResponseException:错误请求

听起来好像不喜欢POST请求。我尝试过不同的方法,包括RESTClient,我试图通过传递文件或作为字符串var来以原始xml格式委托请求。 我不完全理解httpBuilder中post或request方法之间的区别。

如果有人能帮助我指出我做错了会非常感激

def http = new HTTPBuilder('http://some_IP:some_Port/')
http.auth.basic('userName','password')
http.post(path:'/path/ToServlet')

http.post(POST,XML)
{

  delegate.contentType="text/xml"
  delegate.headers['Content-Type']="text/xml"
  //delegate.post(getClass().getResource("/query.xml"))
 // body = getClass().getResource("/query.xml")
   body = 
   {
      mkp.xmlDeclaration()

        Request{
          Header{
                     Command('Retrieve')
                     EntityIdentifiers
                     {
                       Identifier(Value:'PhoneNumber', Type:'TelephoneNumber')
                      }
                                  EntityName('Subscriber')
                  }
         }
   }
}

现在,如果我在我的请求中翻译了XML错误,那么它的XML版本就是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Provisioning xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Request>
    <Header>
        <Command>Retrieve</Command>
        <EntityIdentifiers>
            <Identifier Value="phoneNumber" Type="TelephoneNumber" />
        </EntityIdentifiers>
        <EntityName>Subscriber</EntityName>
    </Header>
</Request>
</Provisioning>

2 个答案:

答案 0 :(得分:1)

好的,所以过了一段时间我收集的解决方案是将groovy库升级到2.4和httpClient4.0.1以及httpBuilder 4.x

这似乎已经成功了,因为我最初使用groovy 1.5.5 您拥有的另一个选择是将Java和Groovy一起用于Java以使用URL和HttpURLConnection类建立连接,希望有足够的示例。 请注意,这不是SSL,如果您想使用SSL和https执行此操作,可能需要采取额外步骤

import groovy.xml.*
import groovy.util.XmlSlurper.*
import groovy.util.slurpersupport.GPathResult.*


import groovyx.net.http.HTTPBuilder

import groovyx.net.http.EncoderRegistry
import java.net.URLEncoder
import java.net.URLEncoder.*
import org.apache.http.client.*
import org.apache.http.client.HttpResponseException
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.conn.ssl.*
import org.apache.http.conn.ssl.TrustStrategy

import static java.net.URLEncoder.encode
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.HTTPBuilder.request
import static groovyx.net.http.Method.POST
//import static groovyx.net.http.RESTClient.*


def SMSCmirrorServerTor = "http://IP:PORT"
def SMSCmirrorServerMntrl = "http://IP:PORT"


def msisdn = args[0]

//Connect to method HTTP xml URL encoded request, XML response
def connectSMSC(server, MSISDN)
{
  // Variables used for the response
  def result = ""
  //Variables used for request
  // one way is to use raw xml
  def rawXML = """
  <Provisioning xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
          <Request>
                  <Header>
                          make sure your <xml> is well formatted use xml formatter online if need be
                  </Header>
          </Request>
  </Provisioning>"""

    def http = new HTTPBuilder(server)
    http.auth.basic('username','password')
    http.contentType = TEXT
    http.headers = [Accept: 'application/xml', charset: 'UTF-8']

    http.request(POST)
    {
      uri.path = 'Servlet or WS/Path'
      requestContentType = URLENC
      body = "provRequest=" + encode(rawXML,"UTF-8")


      response.success = { resp, xml ->

        def xmlParser = new XmlSlurper().parse(xml)
        //Helps if you know in advance the XML response tree structure at this point it's only xmlSlurper doing the work to parse your xml response
        def ResponseStatus = xmlParser.Response.Header.ResponseStatus
        result += "Response Status: " + ResponseStatus.toString() + "\n================\n"

        def ResponseHeader = xmlParser.Response.Header.Errors.Error
        ResponseHeader.children().each
        {
          result += "\n${it.name()}: " + it.text().toString() + "\n"

        }

        def xmlDataRootNode = xmlParser.Response.Data.Subscriber
        xmlDataRootNode.children().each
        {

          if (it.name() == 'UserDevices')
          {
            result += "\n" + it.UserDevice.name() + " :" + "\n-----------------\n"
            it.UserDevice.children().each
            {
              result += it.name() + " :" + it.text().toString() + "\n"
            }
          }
          else
          {
             result += "\n${it.name()}: " + it.text().toString() + "\n"
          }

        }//End of each iterator on childNodes

        if (ResponseStatus.text().toString() == "Failure")
        {
          def ErrorCode = resp.status
          result += "Failed to process command. Bad request or wrong input"
          //result += "\nHTTP Server returned error code: " + ErrorCode.toString()
          // Note this error is for bad input server will still return status code 200 meaning o.k. unlike err 401 unathorized or err 500 those are http errors so be mindful of which error handling we're talking about here application layer vs http protocol layer error handling
        }

      }//End of response.success closure
    }//End of http.request(POST) method 

  result
}//end of connectSMSC method


//the following is only in case server A is down we can try server B 
def finalResponse=""
try
{
  finalResponse += connectSMSC(SMSCmirrorServerTor,msisdn)

}

catch(org.apache.http.conn.HttpHostConnectException e)
{

  finalResponse += "Encountered HTTP Connection Error\n ${e}\n Unable to connect to host ${SMSCmirrorServerTor}\n Trying to connect to mirror server ${SMSCmirrorServerMntrl} instead\n"
  finalResponse += connectSMSC(SMSCmirrorServerMntrl,msisdn)
}
catch (groovyx.net.http.HttpResponseException ex)
{

  finalResponse += "Encountered HTTP Connection Error Code: ${ex.statusCode}\n"
  finalResponse += connectSMSC(SMSCmirrorServerMntrl,msisdn)
}
/*
*
* java.lang.IllegalArgumentException: port out of range:

*/
catch(java.lang.IllegalArgumentException e)
{
 finalResponse += "Error: " + e
}

catch(java.io.IOException e)
{

  finalResponse += "IO Error: " + e
  finalResponse += connectSMSC(SMSCmirrorServerMntrl,msisdn)
}
catch(java.io.FileNotFoundException e)
{

  finalResponse += "IO Error: " + e
  finalResponse += connectSMSC(SMSCmirrorServerMntrl,msisdn)
}


println finalResponse.toString()

答案 1 :(得分:0)

看起来你正在发布请求,但两者都没有正确形成。尝试这样的事情:

def writer = new StringWriter()

def req = new MarkupBuilder(writer)
req.mkp.xmlDeclaration(version:"1.0", encoding:"utf-8")
req.Provisioning {
    Request {
        Header {
            Command('Retrieve')
            EntityIdentifiers {
                Identifier(Value:'PhoneNumber', Type:'TelephoneNumber')
            }
            EntityName('Subscriber')
        }
    }
}

def http = new HTTPBuilder('http://some_IP:some_Port/path/ToServlet')
http.auth.basic('userName','password')
http.post(contentType:XML, query:writer) { resp, xml ->
    // do something with response
}