|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
HttpClient gives HTTP 404 after multipart form submitHello.
I'm using HttpClient 4.0 in my project. Firstly i'm submitting multipart form this way: HttpPost httpPost = new HttpPost("http://[host here]/in.php"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("method", new StringBody("post")); entity.addPart("key", new StringBody("223fwe0923fjf23")); FileBody fileBody = new FileBody(new File("photo.jpg"), "image/jpeg"); entity.addPart("file", fileBody); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity result = response.getEntity(); String responseString = ""; if (result != null) { InputStream inputStream = result.getContent(); byte[] buffer = new byte[1024]; while(inputStream.read(buffer) > 0) responseString += new String(buffer); result.consumeContent(); } This performs successfully. Then I'm getting some status info about form i've just submitted. I do it like this: HttpGet httpGet = new HttpGet("http://[host here]/res.php?key="+myKey+"&action=get&id="+id); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); execute() method throws ClientProtocolException. Using log4j i knew that web server says about HTTP 404 error. Wierd detail: DEBUG [org.apache.http.wire] >> "GET /res.php?key=46a350bd56f9&action=get&id=89619587[0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0] .... Endless nulls in the end of request that are not concerned with my id variable. One more detail: if i comment lines where i use FileBody class, all works fine with no any exceptions and HTTP 404. So, the problem isn't web server problem. Am I working with multipart form wrong way? Any help appreciated. Thank you. |
|
|
Re: HttpClient gives HTTP 404 after multipart form submitmtomy wrote:
> Hello. > > I'm using HttpClient 4.0 in my project. Firstly i'm submitting multipart > form this way: > > HttpPost httpPost = new HttpPost("http://[host here]/in.php"); > > MultipartEntity entity = new > MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); > entity.addPart("method", new StringBody("post")); > entity.addPart("key", new StringBody("223fwe0923fjf23")); > FileBody fileBody = new FileBody(new File("photo.jpg"), "image/jpeg"); > entity.addPart("file", fileBody); > httpPost.setEntity(entity); > > HttpResponse response = httpClient.execute(httpPost); > HttpEntity result = response.getEntity(); > > String responseString = ""; > if (result != null) { > InputStream inputStream = result.getContent(); > > byte[] buffer = new byte[1024]; > while(inputStream.read(buffer) > 0) > responseString += new String(buffer); > This is obviously wrong. InputStream#read method does not necessarily fill up the whole buffer. This is a better way of doing the same: ---------------- String responseString = null; if (result != null) { StringBuilder buffer = new StringBuilder(); InputStream inputStream = entity.getContent(); try { String charset = EntityUtils.getContentCharSet(entity); Reader reader = new InputStreamReader(inputStream, charset); int l; char[] tmp = new char[1024]; while ((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } responseString = buffer.toString(); } finally { inputStream.close(); } } ---------------- Oleg > result.consumeContent(); > } > > This performs successfully. > Then I'm getting some status info about form i've just submitted. I do it > like this: > > HttpGet httpGet = new HttpGet("http://[host > here]/res.php?key="+myKey+"&action=get&id="+id); > HttpResponse response = httpClient.execute(httpGet); > HttpEntity entity = response.getEntity(); > > execute() method throws ClientProtocolException. Using log4j i knew that web > server says about HTTP 404 error. > Wierd detail: > DEBUG [org.apache.http.wire] >> "GET > /res.php?key=46a350bd56f9&action=get&id=89619587[0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0][0x0] > .... > Endless nulls in the end of request that are not concerned with my id > variable. > > One more detail: if i comment lines where i use FileBody class, all works > fine with no any exceptions and HTTP 404. So, the problem isn't web server > problem. > > Am I working with multipart form wrong way? Any help appreciated. > Thank you. --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@... For additional commands, e-mail: httpclient-users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |