简体   繁体   English

请求具有自定义标头和授权的HTTP REST资源?

[英]Request HTTP rest resource with custom headers and authorization?

I'm working on a web service and I need a code for making an HTTP request in java using basic authentication. 我正在使用Web服务,需要使用基本身份验证在Java中发出HTTP请求的代码。 I found lots of code at the internet but no one worked as expected. 我在互联网上找到了很多代码,但没有人按预期工作。 Does anyone knows some library and/or link to do this? 有谁知道一些图书馆和/或链接这样做?

Here is a python equivalent to what I want... 这是等效于我想要的python ...

import urllib2
import base64

req = urllib2.Request('http://testeserjaum.appspot.com/rest/metadata')
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
base64string = base64.encodestring('%s:%s' %('user', 'pass'))
print base64string
req.add_header("Authorization", "Basic %s" %base64string)
lol = urllib2.urlopen(req)
print lol.read()

Yea, you just use the HttpURLConnection class. 是的,您只需要使用HttpURLConnection类。 The Android team has actually put a lot of development time into making this class extremely performant and usefully. Android团队实际上花费了大量的开发时间来使这个类非常高效和有用。 I highly recommend it! 我强烈推荐它!

PS If you're really curious, checkout this blog post from the android dev team . PS:如果您真的很好奇,请查看android开发团队的博客文章

You may want to check out Apache HTTP Client (http://hc.apache.org/httpcomponents-client-ga/). 您可能要签出Apache HTTP客户端(http://hc.apache.org/httpcomponents-client-ga/)。 It makes HTTP communication pretty simple. 它使HTTP通信非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM