Java HTTP Post post(String url, String postdata, String cookie)

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

Description

post

License

Open Source License

Declaration

public static HttpURLConnection post(String url, String postdata,
            String cookie) 

Method Source Code

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

import java.io.IOException;

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

public class Main {
    public static HttpURLConnection post(String url, String postdata,
            String cookie) {/*w  ww. ja  v a 2s .  c  om*/
        HttpURLConnection pHuc = getConnection(url);

        pHuc.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        pHuc.setRequestProperty("Origin",
                url.substring(0, url.indexOf("/")));
        pHuc.setRequestProperty("Host", url.substring(0, url.indexOf("/")));
        pHuc.setRequestProperty("Cookie", cookie);

        try {
            pHuc.setRequestMethod("POST");
            pHuc.setDoOutput(true);
            pHuc.getOutputStream().write(
                    postdata == null ? "".getBytes() : postdata.getBytes());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return pHuc;
    }

    public static HttpURLConnection getConnection(String url) {
        try {
            return (HttpURLConnection) new URL(
                    url.startsWith("http://") ? url : "http://" + url)
                    .openConnection();
        } catch (Exception e) {

        }
        return null;
    }
}

Related

  1. post(String url, int timeout, Map header)
  2. post(String url, JsonNode body)
  3. post(String url, Map params, String charset)
  4. post(String url, String body, Map headers)
  5. post(String url, String payload)
  6. Post(URL url, Map fields)
  7. post(URL url, Map values)
  8. post(URL url, String content)
  9. post(URL url, String contentType, byte[] data)