Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Java connection refused #40

Open
GoogleCodeExporter opened this issue Mar 30, 2015 · 0 comments
Open

Java connection refused #40

GoogleCodeExporter opened this issue Mar 30, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. run the code I have provided below

What is the expected output? What do you see instead?
I expect a standard return from the ImageshackAPI instead I recieve that the 
connection have been refused. I presume this is due to me not knowing what type 
the file should be in when I upload.

Code:

    public String ImageShack (String imageDir, String myKey) {

        //set file
        BufferedImage image = null;
        File file = new File(imageDir);

        //Set namevalue pairs
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        try
        {
            image = ImageIO.read(file);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            ImageIO.write(image, "png", byteArray);
            byteArray.flush();
            //byte[] byteImage = byteArray.toByteArray();
            Base64 base = new Base64(false);
            String dataImage = base.encodeAsString(byteArray.toByteArray());
            byteArray.close();
            dataImage = java.net.URLEncoder.encode(dataImage, "ISO-8859-1");

            //Assign name valued pars
            params.add(new BasicNameValuePair("key", myKey));
            params.add(new BasicNameValuePair("fileupload", dataImage));
            params.add(new BasicNameValuePair("format", "json"));

            //Create HTTPClient and Client
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.imageshack.us/upload_api.php");

            //set entities
           httpPost.setEntity(new UrlEncodedFormEntity(params));

            //Execute & get response
            HttpResponse response = httpClient.execute(httpPost);
            return response.toString();

        }
        catch(Exception e)
        {
                return "Error: " + e.getMessage();
    }
    }

Original issue reported on code.google.com by [email protected] on 18 Jan 2013 at 12:50

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant