繁体   English   中英

Java中的httpURLconnection以ASCII响应?

[英]response in ASCII from httpURLconnection in Java?

有可能以ASCII格式转换HttpURLconnection响应吗? 我可以得到它的示例代码吗?

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();

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)

暂无
暂无

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

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