File Upload Issue

Put your problem here if it does not fit any of the other categories.

File Upload Issue

Postby umair » Mon Oct 05, 2009 5:02 am

Hi guys

I've made a method which sends POST calls to a web-server. All of my calls are working absolutely fine but i get problem while Uploading a File. First, check out the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static String callAction(String soapAction, String soapRequest) throws Exception {
  2.                 String xmlResponse = "";
  3.                 try {
  4.                         URL url = new URL(Constants.Host_NAME);
  5.                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  6.  
  7.                         // Modify connection settings
  8. //                      connection.setReadTimeout(8000);
  9.                         connection.setRequestMethod("POST");
  10.                         connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  11.                         connection.setRequestProperty("Content-Length", soapRequest.length() + "");
  12.                         connection.setRequestProperty("SOAPAction", soapAction);
  13.                         // Enable reading and writing through this connection
  14.                         connection.setDoInput(true);
  15.                         connection.setDoOutput(true);
  16.  
  17.                         // Connect to server
  18.                         connection.connect();
  19.  
  20.                         // Send the request
  21.                         BufferedWriter writer = new BufferedWriter(new
  22.                             OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
  23.                         writer.write(soapRequest);
  24.                         writer.flush();
  25.                         writer.close();
  26.  
  27.                         // Read server's response
  28.                         BufferedReader reader = new BufferedReader(new
  29.                             InputStreamReader(connection.getInputStream()));
  30.                         String line;
  31.  
  32.                         while ((line = reader.readLine()) != null) {
  33.                                 xmlResponse = xmlResponse.concat(line);
  34.                         }
  35.  
  36.                         // Disconnect from server
  37.                         connection.disconnect();
  38.  
  39.                 } catch (MalformedURLException mue) {
  40.                         throw new FAWException(ConnectionManager.class.getSimpleName(), mue.getMessage(), mue
  41.                                         .getStackTrace());
  42.                 } catch (UnknownHostException uhe) {
  43.                         throw new FAWException(ConnectionManager.class.getSimpleName(), uhe.getMessage(), uhe
  44.                                         .getStackTrace());
  45.                 } catch (SocketException se) {
  46.                         throw new FAWException(ConnectionManager.class.getSimpleName(), se.getMessage(), se
  47.                                         .getStackTrace());
  48.                 } catch (IOException ioe) {
  49.                         throw new FAWException(ConnectionManager.class.getSimpleName(), ioe.getMessage(), ioe
  50.                                         .getStackTrace());
  51.                 } catch (Exception e) {
  52.                         throw new FAWException(ConnectionManager.class.getSimpleName(), e.getMessage(),
  53.                             e.getStackTrace());
  54.                 }
  55.                 return xmlResponse;
  56.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


The problem occurs when I open the InputStream from the connection. OutputStream writes and closes successfully but on opening InputStream Content-Length Exceeded exception is thrown.

Does this have anything to do with the data length? I'm sending 1KB data at a time. Please suggest what am I missing.
Muhammad Umair
User avatar
umair
Developer
Developer
 
Posts: 28
Joined: Thu Feb 26, 2009 1:00 pm
Location: Lahore, Pakistan

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests