Java HTTP Post httpPostText(String urlStr, String textString)

Here you can find the source of httpPostText(String urlStr, String textString)

Description

http Post Text

License

Apache License

Declaration

public static String httpPostText(String urlStr, String textString) throws Exception 

Method Source Code


//package com.java2s;
/*// w  w  w  .  j  ava  2s.c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String httpPostText(String urlStr, String textString) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/plain");
        conn.setRequestProperty("Accept", "application/json");
        conn.setDoOutput(true);
        OutputStream out = conn.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        writer.write(textString);
        writer.close();
        out.close();
        return getHttpResponse(conn, 200);
    }

    private static String getHttpResponse(HttpURLConnection conn, int expectedReturnCode) throws IOException {
        System.out.println("response code:" + conn.getResponseMessage());
        if (conn.getResponseCode() != expectedReturnCode) {
            System.out.println("Custom Error:" + conn.getResponseCode());
            throw new IOException(conn.getResponseMessage());
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        conn.disconnect();
        return sb.toString();
    }
}

Related

  1. httpGet(final String httpurl, final Map... requestProperties)
  2. httpPost(final String httpurl, final String data, final Map... requestProperties)
  3. httpPost(String httpUrl)
  4. httpPost(String url, StringBuffer data)
  5. httpPost(String urlToRead, String post)
  6. post(String json, String url)
  7. post(String json, String url)
  8. post(String rawUrl, String body)
  9. post(String url, int timeout, Map header)