2016-01-02 108 views
1

我有要求,我需要获取数据格式salesforce数据库。我的输入ID将超过1000+。因此,我想通过post方法传递这个ID列表。向java发送postforce请求

GET方法失败,因为它超出了限制。

有人可以帮助我吗?

回答

1

我假设你的问题,一些(但不是全部)的GET请求已经正常工作,所以你已经有大部分需要与SalesForce交谈的代码,你只需要填补如何使差距POST请求而不是GET请求。

我希望下面的代码提供了一些演示。需要注意的是未经检验的,因为我现在并没有访问Salesforce的实例来测试它反对:

import org.apache.http.HttpHeaders; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.ContentType; 
import org.apache.http.message.BasicNameValuePair; 

import java.nio.charset.StandardCharsets; 
import java.util.ArrayList; 
import java.util.List; 

public class HttpPostDemo { 

    public static void main(String[] args) throws Exception { 

     String url = ... // TODO provide this. 

     HttpPost httpPost = new HttpPost(url); 
     // Add the header Content-Type: application/x-www-form-urlencoded; charset=UTF-8. 
     httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8).getMimeType()); 

     // Construct the POST data. 
     List<NameValuePair> postData = new ArrayList<>(); 
     postData.add(new BasicNameValuePair("example_key", "example_value")); 
     // add further keys and values, the one above is only an example. 

     // Set the POST data in the HTTP request. 
     httpPost.setEntity(new UrlEncodedFormEntity(postData, StandardCharsets.UTF_8)); 

     // TODO make the request... 
    } 
} 

或许值得指出的是,在本质上的代码是没有太大的不同,其出现在that in a related question侧边栏。

+0

感谢您的回答。在这里我无法使用execute()。当我尝试初始化为HttpClient httpClient = new DefaultHttpClient();它显示为被贬低。你能建议我吗? @Luke Woodward –

+0

@DavidSam:你可以使用'HttpClient httpclient = HttpClients.createDefault();'在我链接到的答案中吗? –