使用apache HttpRequest为opart签署多部分请求

时间:2014-02-01 20:35:39

标签: oauth httprequest multipart

enter code here我需要向使用oAuth签名的其他api发送多部分请求

在我的pom中我正在使用

 <dependency>
            <groupId>oauth.signpost</groupId>
            <artifactId>signpost-commonshttp4</artifactId>
            <version>1.2.1.1</version>
 </dependency> 

我正在使用此代码添加多部分表单

consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
      consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        consumer.setTokenWithSecret("XXXX", "YYYY");
        System.out.println("Fetching request token from "
                + REQUEST_TOKEN_ENDPOINT);




PostMethod filePost = new PostMethod("http://..../");               
                Part[] parts = {
                        new FilePart("metadata", temp, "application/xml", "UTF8"),
                        new FilePart("attachment", imageFile, "image/jpeg", null),                      
                };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

                 HttpClient client = new HttpClient(); 

                 int status = client.executeMethod(filePost);

问题是要执行consumer.sign(request),使用oAuth签署请求,我需要有一个HttpRequest ...

所以我的问题是,我该怎样做才能使用来自apache的HttpRequest发送类似类似的多部分POST请求。

由于

1 个答案:

答案 0 :(得分:0)

好吧,我回答了自己,因为我解决了我的问题。我在这里贴了怎么做,希望它对某些身体有用

HttpPost uploadBackgroundPost = new HttpPost ("http://.../");
                 consumer.sign(uploadBackgroundPost);
                 MultipartEntity entity = new MultipartEntity (HttpMultipartMode.STRICT); 

                                                FileBody tempBody = new FileBody(temp, "application/xml"); 
                                                 FileBody imageBody = new FileBody(imageFile, "image/jpeg"); 
                                                 entity.addPart("metadata", tempBody);
                                                 entity.addPart("attachment", imageBody); 
                                                 uploadBackgroundPost.setEntity(entity); 
                                                 DefaultHttpClient httpClient = new DefaultHttpClient(); 

                                                 System.out.println(httpClient.execute(uploadBackgroundPost, new BasicResponseHandler()));