http-https client.java

来自「HTTP HTTPS client and more」· Java 代码 · 共 23 行

JAVA
23
字号
Get response text from HTTP/HTTPS sites and some header data

-------
public String getResponse(String urlString) throws Exception{// -throws Exception- not -try catch- So the caller can know if any error happened
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();// Open connection with the server
    conn.setDoInput (true);
    conn.setDoOutput (true);
    conn.setUseCaches (false);// No need for caching

    String tempStatus = conn.getHeaderField(0);// Status field

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));// To get input returned from connection
    String temp;// To read line by line string response in this temp string
    StringBuffer response = new StringBuffer();// To collect all line response
    while ((temp = in.readLine()) != null){
        response.append(temp);
        response.append("\n");
    }
    in.close();// Close the input reader

    return response.toString();// that's the respone
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?