我正在尝试使用 POST 向 Web 发送 HTTP 请求,我想模仿此表单的行为并获取生成的 HTML 代码。
<form action="http://www.myweb.com/index.php?route=account/login" method="post" enctype="multipart/form-data">
<div class="content">
<p>Ya soy cliente</p>
<b>Dirección e-mail:</b><br>
<input type="text" name="email" value="">
<br>
<br>
<b>Contraseña:</b><br>
<input type="password" name="password" value="">
<br>
<a href="http://www.myweb.com/index.php?route=account/forgotten">Contraseña olvidada</a><br>
<br>
<input type="submit" value="Entrar" class="button">
</div>
</form>
到目前为止,我已经使用了来自 apache 的 HttpClient,但不起作用。我收到下一条消息:
executing request POST http://www.myweb.com/index.php?route=account/login HTTP/1.1
HTTP/1.1 200 OK
Response content length: -1
HTTP/1.1 200 OK
Response content length: -1
org.apache.http.conn.EofSensorInputStream@47cdbe2
编码:
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://myweb.com/" +
"index.php?route=account/login");
StringBody email = new StringBody("myemail@email.com");
StringBody pass = new StringBody("mypass");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", email);
reqEntity.addPart("password", pass);
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println(resEntity.getContent());
}
EntityUtils.consume(resEntity);
} finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
知道如何让它工作吗?谢谢