0

I have a httpurlconnection code that is meant to post data to server. When I am trying to send small data it's ok but whene i try to send a base64 image string the string is not received properly at server. I have checked the base64 converted string from jpg and the encoding is ok. So, there is something wrong with the http process that's making the string corrupted. Initially I thought that probably it's the big images that's causing the problem but on using a very small image only transfers part of the image to server and the rest is corrupted. Can somebody suggest me how can I send a base64 image string to server. My code is as below:

HttpURLConnection hpcon= (HttpURLConnection) Url.openConnection();
        hpcon.setRequestMethod("POST");
        hpcon.setConnectTimeout(5 * 1000);
        hpcon.setDoOutput(true);
        OutputStreamWriter osw= new OutputStreamWriter(hpcon.getOutputStream());
        osw.write(data);
        osw.close();

The data is something like:

key=YeP1r&nodeId=5&typeId=9&status=0&eventImage=imageString

user1900588
  • 181
  • 1
  • 3
  • 15
  • @Anirudh it's other parameters like key used and base64 image string and other such parameters required by server for processing. – user1900588 Apr 22 '13 at 06:27
  • 1
    you are not setting any content-type, have you tried setting `Content-Type` explicitly? – harsh Apr 22 '13 at 06:31
  • should it be "text/plain" or I need to add something specific for base64? hpcon.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); – user1900588 Apr 22 '13 at 06:36
  • check this quesion http://stackoverflow.com/questions/9210332/post-data-java-with-httpurlconnection – Nandkumar Tekale Apr 22 '13 at 06:37
  • @NandkumarTekale already did that. I don't know why but everytime I add a line to my code that tries to specify any content type-either utf or multipart-no image is posted to the server. Infact no request is sent to the server. if I remove those lines the image is sent but as mentioned before it is corrupted. – user1900588 Apr 22 '13 at 06:42
  • should be worth looking at http://stackoverflow.com/questions/10104499/response-write-base64-string – harsh Apr 22 '13 at 06:52

1 Answers1

0

Couldn't find a straight solution using HttpUrlConnection. Used: httpClient from apache. Actually pretty easy to use.
Last example multipart

user1900588
  • 181
  • 1
  • 3
  • 15