By Niko Gamulin

2010-01-08 08:18:52 8 Comments

I would like to generate POST request to a server which requires authentication. I tried to use the following method:

private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) {

    String data = "product[title]=" + URLEncoder.encode(title) +
                "&product[content]=" + URLEncoder.encode(content) + 
                "&product[price]=" + URLEncoder.encode(price.toString()) +
                "&tags=" + tags;
    try {
        URL url = new URL(urlString);
        URLConnection conn;
        conn = url.openConnection();
        conn.setRequestProperty ("Authorization", "Basic " + encodedString);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        // Get the response 
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        String line; 
        while ((line = rd.readLine()) != null) { 
            // Process line... 
        return rd.toString();
    } catch (MalformedURLException e) {

        return e.getMessage();
    catch (IOException e) {

        return e.getMessage();

but the server doesn't receive the authorization data. The line which is supposed to add authorization data is the following:

conn.setRequestProperty ("Authorization", "Basic " + encodedString);

and the line

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

also throws an IOException.

Anyway I would be very thankful if anyone could suggest any fix of the logic above in order to enable authorization using POST with UrlConnection.

but obviously it doesn't work as it is supposed to although if the same logic is used for GET request everything works fine.


@Adeel Ansari 2010-01-08 08:27:19

A fine example found here. Powerlord got it right, below, for POST you need HttpURLConnection, instead.

Below is the code to do that,

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    conn.setRequestProperty ("Authorization", encodedCredentials);

    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    String line;
    BufferedReader reader = new BufferedReader(new 
    while ((line = reader.readLine()) != null) {

Change URLConnection to HttpURLConnection, to make it POST request.

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

Suggestion ( comments):

You might need to set these properties too,

conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Accept", "*/*" );

@Niko Gamulin 2010-01-08 10:27:06

This is GET request which is not a problem.

@Adeel Ansari 2010-01-08 12:17:04

Not a big difference in terms of authorization.

@David Newcomb 2012-12-14 19:40:11

It is if you came here looking for how to do the post bit :(

@Bob 2014-03-10 14:24:04

This answer is not correct: This Answer is:

@Adeel Ansari 2014-07-30 06:51:41

@Stallman: Same as what it means in the original question. You'll find encodedString instead of encodedCredential, which means password, simply.

@Ray Kiddy 2015-09-08 19:19:01

The bit above did not work when I tried it. I had to add set the following on the connection: uc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); uc.setRequestProperty( "Accept", "*/*" );

@Yousha Aleayoub 2016-04-18 18:36:57

setDoOutput(true) forces UrlConnection to POST... right?

@Adeel Ansari 2016-06-06 04:26:29

@YoushaAleayoub: Nope.

@Yousha Aleayoub 2016-06-17 11:44:52

@AdeelAnsari, No? so what is this:…

@Adeel Ansari 2016-06-20 04:29:14

@YoushaAleayoub, It's just stating that it's used to request body, which is usually the case with POST -- and PUT, not just POST. So, we can't say it forces to POST, that's what it does in practice though; but taking it for granted would be wrong. The right thing to use is httpUrlConn.setRequestMethod("POST").

@Yousha Aleayoub 2016-06-20 12:23:02

@AdeelAnsari, good point and thanks for reply... but what if we use URLConnection, not HTTPURLConnection?

@Adeel Ansari 2016-06-21 03:03:53

@YoushaAleayoub, Why stick with URLConnection, when you really want to POST. Beware, HEAD, GET, POST, PUT etc., all are HTTP's methods, or might also be of some other protocol; but general URL doesn't care.

@yubaraj poudel 2015-08-02 11:34:46

On API 22 The Use Of BasicNamevalue Pair is depricated, instead use the HASMAP for that. To know more about the HasMap visit here more on hasmap

package com.yubraj.sample.datamanager;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.yubaraj.sample.utilities.GeneralUtilities;

import java.util.HashMap;
import java.util.Map;


 * Created by yubraj on 7/30/15.
public class ServerRequestHandler {
    private static final String TAG = "Server Request";
    OnServerRequestComplete listener;

    public ServerRequestHandler (){

    public void doServerRequest(HashMap<String, String> parameters, String url, int requestType, OnServerRequestComplete listener){

        debug("ServerRequest", "server request called, url  = " + url);
        if(listener != null){
            this.listener = listener;
        try {
            new BackgroundDataSync(getPostDataString(parameters), url, requestType).execute();
            debug(TAG , " asnyc task called");
        } catch (Exception e) {

    public void doServerRequest(HashMap<String, String> parameters, String url, int requestType){
        doServerRequest(parameters, url, requestType, null);

    public interface OnServerRequestComplete{
        void onSucess(Bundle bundle);
        void onFailed(int status_code, String mesage, String url);

    public void setOnServerRequestCompleteListener(OnServerRequestComplete listener){
        this.listener = listener;

    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, String> entry : params.entrySet()){
            if (first)
                first = false;

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));

        return result.toString();

    class BackgroundDataSync extends AsyncTask<String, Void , String>{
        String params;
        String mUrl;
        int request_type;

        public BackgroundDataSync(String params, String url, int request_type){
            this.mUrl = url;
            this.params = params;
            this.request_type = request_type;

        protected void onPreExecute() {

        protected String doInBackground(String... urls) {
            debug(TAG, "in Background, urls = " + urls.length);
            HttpURLConnection connection;
                debug(TAG, "in Background, url = " + mUrl);
                String response = "";
                switch (request_type) {
                    case 1:
                        try {
                            connection = iniitializeHTTPConnection(mUrl, "POST");
                            OutputStream os = connection.getOutputStream();
                            BufferedWriter writer = new BufferedWriter(
                                    new OutputStreamWriter(os, "UTF-8"));
                            int responseCode = connection.getResponseCode();
                            if (responseCode == HttpsURLConnection.HTTP_OK) {
                           /* String line;
                            BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                            while ((line=br.readLine()) != null) {
                                response = getDataFromInputStream(new InputStreamReader(connection.getInputStream()));
                            } else {
                                response = "";
                        } catch (IOException e) {
                    case 0:
                        connection = iniitializeHTTPConnection(mUrl, "GET");

                        try {
                            if (connection.getResponseCode() == connection.HTTP_OK) {
                                response = getDataFromInputStream(new InputStreamReader(connection.getInputStream()));
                        } catch (Exception e) {
                            response = "";
                return response;


        protected void onPostExecute(String s) {
            if(TextUtils.isEmpty(s) || s.length() == 0){
                listener.onFailed(DbConstants.NOT_FOUND, "Data not found", mUrl);
                Bundle bundle = new Bundle();
                bundle.putInt(DbConstants.STATUS_CODE, DbConstants.HTTP_OK);
                bundle.putString(DbConstants.RESPONSE, s);
                bundle.putString(DbConstants.URL, mUrl);
            //System.out.println("Data Obtained = " + s);

        private HttpURLConnection iniitializeHTTPConnection(String url, String requestType) {
            try {
                debug("ServerRequest", "url = " + url + "requestType = " + requestType);
                URL link = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) link.openConnection();
                return conn;
            catch(Exception e){
            return null;

    private String getDataFromInputStream(InputStreamReader reader){
        String line;
        String response = "";
        try {

            BufferedReader br = new BufferedReader(reader);
            while ((line = br.readLine()) != null) {
                response += line;

                debug("ServerRequest", "response length = " + response.length());
        catch (Exception e){
        return response;

    private void debug(String tag, String string) {
        Log.d(tag, string);

and Just call the function when you needed to get the data from server either by post or get like this

HashMap<String, String>params = new HashMap<String, String>();
                    params.put("action", "request_sample");
                    params.put("name", uname);
                    params.put("message", umsg);
                    params.put("email", getEmailofUser());
                    params.put("type", "bio");
dq.doServerRequest(params, "your_url", DbConstants.METHOD_POST);
                    dq.setOnServerRequestCompleteListener(new ServerRequestHandler.OnServerRequestComplete() {
                        public void onSucess(Bundle bundle) {
                            debug("data", bundle.getString(DbConstants.RESPONSE));

                        public void onFailed(int status_code, String mesage, String url) {
                            debug("sample", mesage);


Now it is complete.Enjoy!!! Comment it if find any problem.

@lejallec 2014-07-19 02:49:52

To do oAuth authentication to external app (INSTAGRAM) Step 3 "get the token after receiving the code" Only code below worked for me

Worth to state also that it worked for me using some localhost URL with a callback servlet configured with name "callback in web.xml and callback URL registered: e.g. localhost:8084/MyAPP/docs/insta/callback

BUT after successfully completed authentication steps, using same external site "INSTAGRAM" to do GET of Tags or MEDIA to retrieve JSON data using initial method didn't work. Inside my servlet to do GET using url like e.g. only method found HERE worked

Thanks to all contributors

        URL url = new URL(httpurl);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("client_id", id);
        params.put("client_secret", secret);
        params.put("grant_type", "authorization_code");
        params.put("redirect_uri", redirect);
        params.put("code", code);  // your INSTAGRAM code received 
        Set set = params.entrySet();
        Iterator i = set.iterator();
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, String> param : params.entrySet()) {
            if (postData.length() != 0) {
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
        System.out.println("INSTAGRAM token returned: "+builder.toString());

@Harvinder 2014-04-15 03:36:43

I ran into this problem today and none of the solutions posted here worked. However, the code posted here worked for a POST request:

// HTTP POST request
private void sendPost() throws Exception {

    String url = "";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

    // Send post request
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {

    //print result


It turns out that it's not the authorization that's the problem. In my case, it was an encoding problem. The content-type I needed was application/json but from the Java documentation:

static String encode(String s, String enc)
Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.

The encode function translates the string into application/x-www-form-urlencoded.

Now if you don't set a Content-Type, you may get a 415 Unsupported Media Type error. If you set it to application/json or anything that's not application/x-www-form-urlencoded, you get an IOException. To solve this, simply avoid the encode method.

For this particular scenario, the following should work:

String data = "product[title]=" + title +
                "&product[content]=" + content + 
                "&product[price]=" + price.toString() +
                "&tags=" + tags;

Another small piece of information that might be helpful as to why the code breaks when creating the buffered reader is because the POST request actually only gets executed when conn.getInputStream() is called.

@Dimitris 2014-02-28 09:45:03

To send a POST request call:

        connection.setDoOutput(true); // Triggers POST.

If you want to sent text in the request use: wr = new;

@Würgspaß 2018-08-15 12:28:52

nope. connection.setDoOutput(true); does not trigger POST. Add this line System.out.println(((HttpURLConnection)connection).getReques‌​tMethod()); and you will read GET.

@Powerlord 2010-01-29 20:39:19

I don't see anywhere in the code where you specify that this is a POST request. Then again, you need a to do that.

In fact, I highly recommend using HttpURLConnection instead of URLConnection, with conn.setRequestMethod("POST"); and see if it still gives you problems.

@Bob 2014-03-10 14:22:44

This is the correct answer. You can not make a post request with URLConnection. You must use HttpURLConnection.

@Oscar 2010-01-29 20:18:34

i was looking information about how to do a POST request. I need to specify that mi request is a POST request because, i'm working with RESTful web services that only uses POST methods, and if the request isn't post, when i try to do the request i receive an HTTP error 405. I assure that my code isn't wrong doing the next: I create a method in my web service that is called through GET request and i point my application to consume that web service method and it works. My code is the next:

    URL server = null;
    URLConnection conexion = null;
    BufferedReader reader = null;
    server = new URL("http://localhost:8089/myApp/resources/webService");
    conexion = server.openConnection();
    reader = new BufferedReader(new InputStreamReader(server.openStream()));

@jarnbjo 2010-01-08 12:07:38

HTTP authorization does not differ between GET and POST requests, so I would first assume that something else is wrong. Instead of setting the Authorization header directly, I would suggest using the class, but I am not sure if it solves your problem. Perhaps your server is somehow configured to require a different authorization scheme than "basic" for post requests?

@Powerlord 2010-01-29 20:29:50

Nifty, I never knew about this class. However, this class is abstract, so he'd actually need an implementing class for it.

@jarnbjo 2010-01-29 21:45:02

Sure, you need to implement an application specific way to get the credentials. In an interactive application, you may e.g. need a popup dialog.

Related Questions

Sponsored Content

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3993427 View
  • 3455 Score
  • 66 Answer
  • Tags:   java random integer

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

90 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

42 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

8 Answered Questions

[SOLVED] How are parameters sent in an HTTP POST request?

30 Answered Questions

[SOLVED] JavaScript post request like a form submit

11 Answered Questions

55 Answered Questions

[SOLVED] How to create a memory leak in Java?

34 Answered Questions


  • 2009-03-10 14:25:20
  • alex
  • 2345517 View
  • 5324 Score
  • 34 Answer
  • Tags:   http rest post put

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content