Java 中 httpURLconnection 的 ASCII 响应?

发布于 2024-12-17 16:46:58 字数 72 浏览 1 评论 0原文

是否有可能将 HttpURLconnection 响应转换为 ASCII 格式?我可以有它的示例代码吗?

Any possibilities to convert the HttpURLconnection response in ASCII? Can I have the sample code of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-12-24 16:46:58
HttpURLconnection yourConnection = getConnectionSomeWay();
InputStream inputStream = yourConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream, "US-ASCII");
BufferedReader bufferedReader = new BufferedReader(reader);
System.out.println(bufferedReader.readLine());

或者,更短的版本:

HttpURLconnection yourConnection = getConnectionSomeWay();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(yourConnection.getInputStream(), "US-ASCII"));
System.out.println(bufferedReader.readLine());

或者如果您的意思是用 ASCII 来编写响应,那么基本上是相同的方式:

HttpURLconnection yourConnection = getConnectionSomeWay();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(yourConnection.getOutputStream(), "US-ASCII"));
bufferedWriter.write("Hello world!");
bufferedWriter.flush();
HttpURLconnection yourConnection = getConnectionSomeWay();
InputStream inputStream = yourConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream, "US-ASCII");
BufferedReader bufferedReader = new BufferedReader(reader);
System.out.println(bufferedReader.readLine());

Or, shorter version:

HttpURLconnection yourConnection = getConnectionSomeWay();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(yourConnection.getInputStream(), "US-ASCII"));
System.out.println(bufferedReader.readLine());

Or in case you meant writing response in ASCII, then it's mostly the same way:

HttpURLconnection yourConnection = getConnectionSomeWay();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(yourConnection.getOutputStream(), "US-ASCII"));
bufferedWriter.write("Hello world!");
bufferedWriter.flush();
倒带 2024-12-24 16:46:58

getContent

公共对象 getContent()
抛出 IOException

Retrieves the contents of this URL connection.

This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created:

    If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type.
    If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named:

                 sun.net.www.content.<contentType>


    where <contentType> is formed by taking the content-type string, replacing all slash characters with a period ('.'), and all other non-alphanumeric characters with the underscore character '_'. The alphanumeric characters are specifically the 26 uppercase ASCII letters 'A' through 'Z', the 26 lowercase ASCII letters 'a' through 'z', and the 10 ASCII digits '0' through '9'. If the specified class does not exist, or is not a subclass of ContentHandler, then an UnknownServiceException is thrown. 

Returns:
    the object fetched. The instanceof operator should be used to determine the specific kind of object returned. 
Throws:
    IOException - if an I/O error occurs while getting the content. 
    UnknownServiceException - if the protocol does not support the content type.
See Also:
    ContentHandlerFactory.createContentHandler(java.lang.String), getContentType(), setContentHandlerFactory(java.net.ContentHandlerFactory)

getContent

public Object getContent()
throws IOException

Retrieves the contents of this URL connection.

This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created:

    If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type.
    If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named:

                 sun.net.www.content.<contentType>


    where <contentType> is formed by taking the content-type string, replacing all slash characters with a period ('.'), and all other non-alphanumeric characters with the underscore character '_'. The alphanumeric characters are specifically the 26 uppercase ASCII letters 'A' through 'Z', the 26 lowercase ASCII letters 'a' through 'z', and the 10 ASCII digits '0' through '9'. If the specified class does not exist, or is not a subclass of ContentHandler, then an UnknownServiceException is thrown. 

Returns:
    the object fetched. The instanceof operator should be used to determine the specific kind of object returned. 
Throws:
    IOException - if an I/O error occurs while getting the content. 
    UnknownServiceException - if the protocol does not support the content type.
See Also:
    ContentHandlerFactory.createContentHandler(java.lang.String), getContentType(), setContentHandlerFactory(java.net.ContentHandlerFactory)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文