Java URL Post post(String url, String content)

Here you can find the source of post(String url, String content)

Description

post

License

LGPL

Declaration

public static String post(String url, String content) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String post(String url, String content) throws IOException {
        URL u = new URL(url);
        URLConnection c = u.openConnection();
        c.setDoOutput(true);// ww w .  j av  a  2s . c  om
        writeToStream(c.getOutputStream(), content);
        return getStreamContent(c.getInputStream());
    }

    public static void writeToStream(OutputStream os, String content) throws IOException {
        OutputStreamWriter writer = new OutputStreamWriter(os);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    public static String getStreamContent(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String c = "", line;
        while ((line = reader.readLine()) != null)
            c += "\n" + line;
        return c.substring(1);
    }
}

Related

  1. post(JSONObject json, String url)
  2. post(String url, Map parameters)
  3. post(String url, String content)
  4. post(String urlstr, String[] params)
  5. post(String urlString, HashMap values)
  6. postData(String urlStr, String data)