Java XML Data Type Converter getImgurContent(String clientID, byte[] image)

Here you can find the source of getImgurContent(String clientID, byte[] image)

Description

get Imgur Content

License

Open Source License

Declaration

public static String getImgurContent(String clientID, byte[] image) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.bind.DatatypeConverter;

public class Main {
    public static String getImgurContent(String clientID, byte[] image) throws IOException {
        String imageString = DatatypeConverter.printBase64Binary(image);
        URL url = new URL("https://api.imgur.com/3/image");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imageString, "UTF-8");
        conn.setDoOutput(true);//  w  w  w  .j a v  a  2 s  .  c om
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Client-ID " + clientID);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.connect();
        StringBuilder stb = new StringBuilder();
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            stb.append(line).append("\n");
        }
        wr.close();
        rd.close();
        return stb.toString();
    }
}

Related

  1. generateSalt()
  2. generateSalt(int clientNonceByteCount)
  3. getByteList(String nomal, boolean isDecode)
  4. getByteListStr(List byteList, boolean isEncode)
  5. getDataUrlForBytes(final byte[] bytes)
  6. getLong(byte[] data)
  7. getText(URL url, String user, String password)
  8. getTextAsJavaByteArrayInitializer( String text, String charsetName, int maxArraySize)
  9. getXMLLong(final long value)