2012-11-23 2 views
1

URL 기반 API가있는 webservice와 통신하고 싶습니다.Grails HttpBuilder URL 인코딩 됨 GET

http://api.bla.com/aaa/bbb.ashx?Action=GetSecurityToken&vendorId=3

나는 모든 세부 사항과 함께 브라우저에 URL을 넣고 XML 페이지를 얻을 수 있습니다 : 예를 들어, 나는 다음과 같은 URL이 있습니다.

http = new HTTPBuilder('http://api.bla.com/aaa/bbb.ashx') 
html = http.get(path : '/', query : [Action :"GetSecurityToken", vendorId: "3"])) 
println html 

이 작품을 나던 이유 :

내가 그러므로 내가 다음 코드를 사용하여 내 Grails 애플리케이션에서 XML 페이지를 싶어. 나는 나쁜 요청을 받는다. Grails 컨트롤러의 위 URL에서 xml 페이지를 얻으려면 어떻게해야합니까?

답변

1

기본 URL을 http://api.bla.com/aaa/bbb.ashx으로 정의하고 통화 경로를 /으로 설정 했으므로 최종 URL은 http://api.bla.com/aaa/bbb.ashx/?Action=GetSecurityToken&vendorId=3으로 생각됩니다.

은 (this 예에서 촬영)처럼 기본 URL을 변경해보십시오 :

def http = new HTTPBuilder('http://api.bla.com/aaa') 
http.get(path : '/bbb.ashx', 
      contentType : XML, 
      query : [Action :"GetSecurityToken", vendorId: "3"]) { resp, reader -> 

    println "response status: ${resp.statusLine}" 
    println 'Headers: -----------' 
    resp.headers.each { h -> 
    println " ${h.name} : ${h.value}" 
    } 
    println 'Response data: -----' 
    System.out << reader 
    println '\n--------------------' 
} 
+0

나는 HTTPBuilder에 대한 로깅을 켜고 경로를 설정하는 것을 발견 : '/'실제로 당신에게 도메인 이름 +받는 URI를 얻을 것이다 쿼리 문자열 : http : //api.bla.com/? Action = GetSecurityToken & vendorId = 3'. 세르지오의 솔루션은 효과가 있지만, get 호출에서 경로 매개 변수를 모두 없앨 수 있습니다. 그래서'http.get (query : [Action : "GetSecurityToken", vendorId : "3"]) –