WebクローラーにApacheHttpClient4.0を使用しています。私が奇妙だと思った動作は次のとおりです。HTTPGETメソッドを介してページを取得しようとしており、404HTTPエラーに関する応答を取得しています。しかし、ブラウザを使用してそのページを取得しようとすると、正常に実行されます。
詳細:1。マルチパートフォームを次の方法でサーバーにアップロードします。
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();
}
アップロードは正常に終了します。
Webサーバーからいくつかの結果を取得しています。
HttpGet httpGet = new HttpGet("http://[host here]/res.php?key="+myKey+"&action=get&id="+id); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity();
実行メソッドの実行中にClientProtocolExceptionが発生します。私はlog4jでこの状況をデバッグしていました。サーバーは「404NotFound」と応答します。しかし、私のブラウザは問題なくそのページをロードしてくれます。
誰か助けてもらえますか?
ありがとうございました。