Java HTTP Read getJsonFromUrlWithJsonParameter(String url, String jsonRequest)

Here you can find the source of getJsonFromUrlWithJsonParameter(String url, String jsonRequest)

Description

get Json From Url With Json Parameter

License

Open Source License

Declaration

public static String getJsonFromUrlWithJsonParameter(String url, String jsonRequest) throws IOException 

Method Source Code

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

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getJsonFromUrlWithJsonParameter(String url, String jsonRequest) throws IOException {
        URL urll = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urll.openConnection();

        //conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-Type", "application/json");
        //conn.setDoInput (true);
        conn.setDoOutput(true);/*from www  .  ja  v a  2s  .  com*/
        //conn.connect();
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(jsonRequest);
        out.close();

        /*if (conn.getResponseCode() != 200)
        {
        throw new RuntimeException("Failed : HTTP error code : "
            + conn.getResponseCode());
        }*/

        BufferedReader br;

        try {
            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        } catch (Throwable e) {
            br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        }

        String output;
        StringBuilder stringa = new StringBuilder();
        while ((output = br.readLine()) != null) {
            stringa.append(output);
        }

        conn.disconnect();
        return stringa.toString();
    }
}

Related

  1. getInputStream(String myUrl)
  2. getInputStream(String url)
  3. getInputStreamFromURL(String urlname)
  4. getInputStreamFromUrl(String urlString)
  5. getInputStreamReader(String httpUrl, int timeout)
  6. getJsonString(String url)
  7. getReader(String url)
  8. getResult(String urlStr, String content)
  9. readData(HttpURLConnection conn)