0

(第一个 Stack Overlflow 问题!)我很难根据下面的 Perl 示例在 Java 中编码等效的表单数据 http POST。我什至使用 WireShark 来尝试捕获这个工作示例,这样我就可以研究发布的 XML 数据,这样我就可以用 Java 编写代码,但没有骰子。有谁知道下面代码中的 XML 是什么样子的?我可以很好地进行身份验证,但表单数据在我的 Java 代码中一直被拒绝。

my $ua = LWP::UserAgent->new();
$ua->timeout($MYTIMEOUT);
$ua->credentials("myweb03:80","mydomain.com",$user, $password);

my $response = $ua->post($PEPSURL, 'content-type' => 'form-data',
                                 Content => {
                                 username => $user,
                                 prep_id => $prep_id,
                                 project => $project,
                                 upfile  => [ $uploadfile ],
                                 discussion => $discussion,
                                 silentsave => int($silentFlag)

             });
4

3 回答 3

1

感谢那些回答这个问题的人以及来自http://indiwiz.com/2009/02/11/multi-part-content-upload-in-apache-http-components-http-的关键代码示例,我让它工作了客户/。这是一个如何做一个多部分/表单数据发布的例子,在这个过程中上传一个文本文件:

package upfile;

import java.io.*;
import java.nio.charset.Charset;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Upfile {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("<host>", 80, "<realm>", "basic"),
                new UsernamePasswordCredentials("<username>", "<password>"));
            HttpPost httpPost = new    HttpPost("<url>");
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("username", new StringBody("<username>", Charset.forName("UTF-8")));
            entity.addParPart("id", new StringBody("86815", Charset.forName("UTF-8")));
            entity.addPart("project", new StringBody("GIZMO", Charset.forName("UTF-8")));
            entity.addPart("discussion", new StringBody("Discussion text; uploaded via Java!", Charset.forName("UTF-8")));
            File f = new File("/Users/sjd/myFile.txt");
            FileBody fileBody = new FileBody(f, "text/plain");
            entity.addPart("upfile", fileBody);
            httpPost.setEntity(entity);
            HttpResponse response = httpclient.execute(httpPost);

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + 
                     entity.getContentLength());
                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
                response.getEntity().writeTo(outstream);
                byte[] responseBody = outstream.toByteArray();
                System.out.println(responseBody.toString());
            }
            EntityUtils.consume(entity);
        } finally {        
            httpclient.getConnectionManager().shutdown();
        }
    }
}
于 2012-09-09T17:40:54.047 回答
0

德罗斯特:

您正在使用这种形式的 LWP::UserAgent->post:

$ua->post( $url, $field_name => $value,... Content => \%form )

根据LWP::UserAgent的文档:

此方法将在给定的 $url 上发送一个 POST 请求,其中 %form 或 @form 为填写的表单内容提供键/值对。其他标题和内容选项与 get() 方法相同。

此方法将使用 HTTP::Request::Common 中的 POST() 函数来构建请求。有关如何传递表单内容和其他高级功能的详细信息,请参阅 HTTP::Request::Common。

HTTP::Request::Common

POST 'http://www.perl.org/survey.cgi',
       Content_Type => 'form-data',
       Content      => [ name  => 'Gisle Aas',
                         email => 'gisle@aas.no',
                         gender => 'M',
                         born   => '1964',
                         init   => ["$ENV{HOME}/.profile"],
                       ]

创建:

  POST http://www.perl.org/survey.cgi
  Content-Length: 388
  Content-Type: multipart/form-data; boundary="6G+f"

  --6G+f
  Content-Disposition: form-data; name="name"

  Gisle Aas
  --6G+f
  Content-Disposition: form-data; name="email"

  gisle@aas.no
  --6G+f
  Content-Disposition: form-data; name="gender"

  M
  --6G+f
  Content-Disposition: form-data; name="born"

  1964
  --6G+f
  Content-Disposition: form-data; name="init"; filename=".profile"
  Content-Type: text/plain

  PATH=/local/perl/bin:$PATH
  export PATH

  --6G+f--

所以其他海报是正确的。格式不是 XML。

希望这会有所帮助。

于 2012-08-29T07:32:19.873 回答
0

Java中的post方法没有“内置”。您可以构建一个,或者您可以尝试Apache 的 HttpComponents API,特别是HttpClient

于 2012-08-29T05:52:32.613 回答