Android Http Post sendPostRequest(String path, String params, String encoding)

Here you can find the source of sendPostRequest(String path, String params, String encoding)

Description

send Post Request

Declaration

public static InputStream sendPostRequest(String path, String params,
            String encoding) throws UnsupportedEncodingException,
            MalformedURLException, IOException, ProtocolException 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class Main {
    public static InputStream sendPostRequest(String path, String params,
            String encoding) throws UnsupportedEncodingException,
            MalformedURLException, IOException, ProtocolException {
        byte[] data = params.getBytes(encoding);
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);/*from www .  ja  v  a2s  .  c o m*/
        conn.setRequestProperty("Content-Type",
                "application/x-javascript; charset=" + encoding);
        conn.setRequestProperty("Content-Length",
                String.valueOf(data.length));
        conn.setConnectTimeout(5 * 1000);
        OutputStream outStream = conn.getOutputStream();
        outStream.write(data);
        outStream.flush();
        outStream.close();
        if (conn.getResponseCode() == 200)
            return conn.getInputStream();
        return null;
    }
}

Related

  1. httpPost(String... params)
  2. writeData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName)
  3. prepareAndSendHttpPost(String URI, ArrayList params)
  4. buildNameValuePair( Hashtable httpPost)
  5. executePost(String url, String parameters)