Java发送请求多部分/表单数据

时间:2013-08-07 22:36:50

标签: java post request multipartform-data

我正在尝试使用POST向网络发送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) {}
    }

知道如何让这个工作吗? 感谢

0 个答案:

没有答案