簡體   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