Http Get 请求无效
Http Get request is not working
我正在尝试使用它的 rest api 从 apache ranger 获取 xml 数据。这是我的代码
val httpclient = new DefaultHttpClient()
val auth=new AuthScope(host,AuthScope.ANY_PORT)
val credentials=new UsernamePasswordCredentials(username, password)
httpclient.getCredentialsProvider()
.setCredentials(auth, credentials)
val httpget = new HttpGet("http://localhost:6080/service/public/api/repository/1")
httpget.setHeader("Accept", "application/xml")
val response = httpclient.execute(httpget)
val entity = response.getEntity
if (entity != null) {
val in = new BufferedReader(new InputStreamReader(entity.getContent()))
var line = in.readLine()
var response = new StringBuffer()
while (line != null) {
response.append(line)
line = in.readLine()
}
in.close()
println(response.toString())
}
如果我从浏览器中点击这个 url,它显示的结果很好。但在代码的情况下,它 returns html。
有什么帮助吗?
试试这个代码
val httpclient = new DefaultHttpClient()
val httpget = new HttpGet("url")
httpget.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "pass"),
"UTF-8", false))
httpget.setHeader("Accept", "application/json")
val response = httpclient.execute(httpget)
EntityUtils.toString(response.getEntity())
在请求中设置身份验证。
谢谢
我正在尝试使用它的 rest api 从 apache ranger 获取 xml 数据。这是我的代码
val httpclient = new DefaultHttpClient()
val auth=new AuthScope(host,AuthScope.ANY_PORT)
val credentials=new UsernamePasswordCredentials(username, password)
httpclient.getCredentialsProvider()
.setCredentials(auth, credentials)
val httpget = new HttpGet("http://localhost:6080/service/public/api/repository/1")
httpget.setHeader("Accept", "application/xml")
val response = httpclient.execute(httpget)
val entity = response.getEntity
if (entity != null) {
val in = new BufferedReader(new InputStreamReader(entity.getContent()))
var line = in.readLine()
var response = new StringBuffer()
while (line != null) {
response.append(line)
line = in.readLine()
}
in.close()
println(response.toString())
}
如果我从浏览器中点击这个 url,它显示的结果很好。但在代码的情况下,它 returns html。
有什么帮助吗?
试试这个代码
val httpclient = new DefaultHttpClient()
val httpget = new HttpGet("url")
httpget.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "pass"),
"UTF-8", false))
httpget.setHeader("Accept", "application/json")
val response = httpclient.execute(httpget)
EntityUtils.toString(response.getEntity())
在请求中设置身份验证。
谢谢