Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void connectAndSendHttp(ByteArrayOutputStream baos) {
        try {

            URL url;
            url = new URL("http://10.0.2.2:8080");

            String charset = "UTF-8";

            HttpURLConnection conn;

            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Accept-Charset", charset);
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
            OutputStream output = conn.getOutputStream();
            output.write(baos.toByteArray());
            output.close();
            conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}