2016-01-02 107 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();'在我鏈接到的答案中嗎? –