Apache HttpClient 4.0. Weird behavior.
Posted
by Mikhail T
on Stack Overflow
See other posts from Stack Overflow
or by Mikhail T
Published on 2009-11-04T19:24:02Z
Indexed on
2010/05/29
2:02 UTC
Read the original article
Hit count: 292
java
|httpclient
Hello.
I'm using Apache HttpClient 4.0 for my web crawler. The behavior i found strange is: i'm trying to get page via HTTP GET method and getting response about 404 HTTP error. But if i try to get that page using browser it's done successfully.
Details: 1. I upload multipart form to server 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();
}
Uppload succefully ends.
I'm getting some results from web server:
HttpGet httpGet = new HttpGet("http://[host here]/res.php?key="+myKey+"&action=get&id="+id);
HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity();
I'm getting ClientProtocolException while execute method run. I was debugging this situation with log4j. Server answers "404 Not Found". But my browser loads me that page with no problem.
Can anybody help me?
Thank you.
© Stack Overflow or respective owner