Java HTTP Post sendPost(String url, String data)

Here you can find the source of sendPost(String url, String data)

Description

send Post

License

Apache License

Declaration

public static int sendPost(String url, String data) throws IOException 

Method Source Code

//package com.java2s;
/**//from  ww w  .  j  a  v a  2 s  .  c om
* Copyright 2016 William Van Woensel
    
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
* 
* 
* @author wvw
* 
*/

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static int sendPost(String url, String data) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("POST");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        return con.getResponseCode();
    }
}

Related

  1. putPOST(HttpURLConnection h, String query)
  2. requestPost(final String remoteUrl, final byte[] content)
  3. sendJson(URL url, String method, String data)
  4. sendJsonData(final HttpURLConnection connection, final String data)
  5. sendPost(String link, String urlParameters)
  6. sendPost(String url, String urlParameters)
  7. sendPost(String webURL, HashMap postOptions)
  8. sendPostRequest(Reader data, URL endpoint, Writer output)
  9. sendPostRequest(Reader data, URL endpoint, Writer output)