Java HTTP Read getResult(String urlStr, String content)

Here you can find the source of getResult(String urlStr, String content)

Description

get Result

License

Open Source License

Declaration

public static String getResult(String urlStr, String content) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

    public static String getResult(String urlStr, String content) {
        URL url = null;/*from   ww w  .  j  a  va  2s. c  o  m*/
        HttpURLConnection connection = null;

        try {
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.connect();

            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(content.getBytes("utf-8"));
            out.flush();
            out.close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
}

Related

  1. getInputStreamFromUrl(String urlString)
  2. getInputStreamReader(String httpUrl, int timeout)
  3. getJsonFromUrlWithJsonParameter(String url, String jsonRequest)
  4. getJsonString(String url)
  5. getReader(String url)
  6. readData(HttpURLConnection conn)
  7. readError(HttpURLConnection conn)
  8. readGetEx(String url)
  9. readHTTPConnection(HttpURLConnection conn)