如何在POST中将Base64图像作为正文发送

时间:2019-02-17 15:00:31

标签: java

我正在寻找使用base64图像的发布请求。当我发出请求时,我从api中收到一个错误,名称为“ ERROR_ZERO_CAPTCHA_FILESIZE”。当我查看它的含义时,它表示:  图像大小小于100个字节。检查图像文件,该帖子返回此错误消息:

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.*;
import java.util.Base64;

public class Main {

    public static void main(String args[]) throws IOException {
        File originalFile = new File("sec_token.php.png");
        String encodedBase64 = null;
        try {
            FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
            byte[] bytes = new byte[(int)originalFile.length()];
            fileInputStreamReader.read(bytes);
            encodedBase64 = new String(Base64.getEncoder().encodeToString(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(encodedBase64);
        HttpURLConnection con = (HttpURLConnection) new URL("http://2captcha.com/in.php?key=61f5d7a6cccc2db4e7c503a59f4f7e&method=base64&imginstructions="+encodedBase64).openConnection();

        String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0";
        System.out.println("User agent: " + USER_AGENT);
        //add request header
        con.setRequestMethod("POST");
        con.setRequestProperty("Host", "2captcha.com");

        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while((inputLine = in.readLine()) != null){
            response.append(inputLine);
        }
        String res = response.toString();
        System.out.println(res);
    }
}

1 个答案:

答案 0 :(得分:0)

我看过API文档,看来您需要对POST正文使用application/x-www-form-urlencodedmultipart/form-data

    File originalFile = new File("sec_token.php.png");
    String encodedBase64 = null;
    try {
        FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
        byte[] bytes = new byte[(int)originalFile.length()];
        fileInputStreamReader.read(bytes);
        encodedBase64 = new String(Base64.getEncoder().encodeToString(bytes));
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(encodedBase64);
    HttpURLConnection con = (HttpURLConnection) new URL("http://2captcha.com/in.php").openConnection();

    String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0";
    System.out.println("User agent: " + USER_AGENT);
    //add request header
    con.setRequestMethod("POST");
    con.setRequestProperty("Host", "2captcha.com");

    String data = "method=base64&body=" + encodedBase64 + "&key=[API_KEY]";
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeChars(data);
    wr.flush();

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

    while((inputLine = in.readLine()) != null){
        response.append(inputLine);
    }

    String res = response.toString();
    System.out.println(res);