2017年8月20日日曜日

【Java】簡易HTTP Post

■3つ程。3番目はcommons-ioが必要。

◆ソースコード
package sample.httppost;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;

import javafx.util.Pair;

public class App {
  public static void main(String[] args) {
    try {
      sendPost1("http://localhost:8090/", "abc");
      System.out.println(sendPost2("http://localhost:8090/", "abc"));
      System.out.println(sendPost3("http://localhost:8090/", "abc", Charset.defaultCharset()));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void sendPost1(String targetUrl, String sendStr) throws IOException {
    URLConnection conn = new URL(targetUrl).openConnection();
    conn.setDoOutput(true);

    try (OutputStream st = conn.getOutputStream()) {
      st.write(sendStr.getBytes());
    }
  }

  public static int sendPost2(String targetUrl, String sendStr) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) new URL(targetUrl).openConnection();
    conn.setDoOutput(true);

    try (OutputStream st = conn.getOutputStream()) {
      st.write(sendStr.getBytes());
    }
    return conn.getResponseCode();
  }


  public static Pair<Integer,String> sendPost3(String targetUrl, String sendStr, Charset chSet) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) new URL(targetUrl).openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept-Charset", chSet.name());
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + chSet.name());
    conn.setRequestProperty("User-Agent", "Mozilla/5.0");

    try (OutputStream st = conn.getOutputStream()) {
      st.write(sendStr.getBytes());
    }

    try (InputStream st = conn.getInputStream()) {
      int code = conn.getResponseCode();
      String str = IOUtils.toString(st, chSet);
      return new Pair<Integer, String>(code, str);
    }
  }
}

◆pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>sample</groupId>
 <artifactId>HttpPost</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>HttpPost</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.5</version>
  </dependency>
 </dependencies>
</project>

0 件のコメント:

コメントを投稿