Java HTTP Read getContentWithPost(String urls, Map pv)

Here you can find the source of getContentWithPost(String urls, Map pv)

Description

get Content With Post

License

Open Source License

Declaration

public static String getContentWithPost(String urls, Map<String, String> pv) 

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.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;

import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import java.util.Map;

public class Main {
    public static String getContentWithPost(String urls, Map<String, String> pv) {
        if (pv.isEmpty())
            return "";
        OutputStreamWriter writer = null;
        BufferedReader reader = null;
        try {//w w  w  .  j  a  va  2  s  .  c  o m
            String body = "";
            for (Map.Entry<String, String> entry : pv.entrySet()) {

                body += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8") + "&";

            }
            //System.out.println(body);
            URL url = new URL(urls);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", String.valueOf(body.length()));

            writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(body);
            writer.flush();

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String content = "";
            for (String line; (line = reader.readLine()) != null;) {
                content += line + "\n";
            }

            return content;
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace(System.out);
        } catch (MalformedURLException mue) {
            mue.printStackTrace(System.out);
        } catch (ProtocolException pe) {
            pe.printStackTrace(System.out);
        } catch (IOException ioe) {
            ioe.printStackTrace(System.out);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException ioe) {
                }
            ;
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException ioe) {
                }
            ;
        }
        return "";
    }
}

Related

  1. getContentLength(URL url)
  2. getContentLength(URLConnection con)
  3. getContents(String urlStr)
  4. getContentTypeFromUrl(String urlname)
  5. getContentTypeSpecified(URLConnection connection)
  6. getData(String urlString)
  7. getHtmlByteArray(final String url)
  8. getHtmlContent(String url)
  9. getInputStream(HttpURLConnection conn)