HttpURLConnection:如何读取答案
我对此快要发疯了。 (正如大多数人在这里发帖哈哈)
我有一个由其他人编写的服务器,我正在向它发送请求以检索信息。
我知道它有效,因为我有其他程序员用来测试的一些网页。
我想直接向那个人问问题,但由于他住在日本,而我住在西班牙,沟通起来有点复杂
,我的请求工作正常,我得到了正确的响应代码,但后来我陷入困境,我不知道如何读取HttpURLConnection对象来获取内容。
在网络浏览器中我得到的结果很简单:
DOC_FOUND
ID
5
AUTHOR
adrian
TITLE
the adrian
DESCRIPTION
我尝试了 getHeaderFields 和 getInputStream 但没有成功(我可能使用它们是错误的)
有什么想法吗?
贾森
I'm going nuts on this. (as most people posting here lol)
I have a server written by somebody else and I'm sending request to it to retrieve info.
I know that it works because I have some webpages that the other programmer used to test.
I would like to ask the question to the guy directly but since he lives in Japan and my in Spain its a bit complicated to communicate
my request works fine and I get the right response code but then I'm stuck, I don't know how to read the HttpURLConnection object to get the content.
in the web browser I get simply :
DOC_FOUND
ID
5
AUTHOR
adrian
TITLE
the adrian
DESCRIPTION
I tried getHeaderFields and getInputStream with no luck (I might be using them wrong)
Any ideas ?
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用某种阅读器包装您的输入流。
Wrap your input stream with some kind of reader.
如果没有代码,我能做的最好的就是向您提供如何正确连接的教程。也许您会注意到您的错误:
如何-open-url-read-contents-httpurl-connection-java
Without code the best I can do is point you to a tutorial of how to properly connect. Maybe you will notice your error:
how-open-url-read-contents-httpurl-connection-java
您是否考虑过使用一个简单的 URLConnection 包装器来为您完成所有繁重的工作?
使用 Resty 可以很简单:
如果您的内容是纯文本;
** 免责声明:我是这个免费开源库的作者。 **
Have you considered a simple wrapper around URLConnection that does all the heavy-lifting for you?
With Resty it could be as simple as:
if your content is plain text;
** DISCLAIMER: I'm the author of this free, open-source library. **