Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有JSON正文和URL参数的HttpClient POST_Java_Json_Httpclient_Apache Nifi_Sfdc - Fatal编程技术网

Java 带有JSON正文和URL参数的HttpClient POST

Java 带有JSON正文和URL参数的HttpClient POST,java,json,httpclient,apache-nifi,sfdc,Java,Json,Httpclient,Apache Nifi,Sfdc,我正在使用NiFi的InvokeHTTP处理器发布到SalesForce端点,特别是登录端点: https://test.salesforce.com/services/oauth2/token 我将client_id、client_secret、username和password字段附加到URL: https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=&client_secret=&use

我正在使用NiFi的InvokeHTTP处理器发布到SalesForce端点,特别是登录端点:

https://test.salesforce.com/services/oauth2/token

我将client_id、client_secret、username和password字段附加到URL:

https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=&client_secret=&username=&password=

此外,还有一条JSON消息/负载通过InvokeHTTP处理器,因此我配置了

内容类型:应用程序/json

当我运行它时,它工作得很好

[注意:那些不知道Apache NiFi但知道Java和/或SFDC中的HttpClient的人可以回答这个问题,我的观点是REST API端点在NiFi上对我有效,但在我尝试使用自定义Java代码访问REST API时不起作用]

现在,因为我想将此机制转换为ExecuteScript处理器中的自定义代码,所以我尝试使用HttpClient库在Java中编码。但根据中的这篇文章,似乎有多种方法可以做到这一点。我先尝试了第4项:

我还在页面中尝试了item#2模式,没有JSON负载,因为参数现在将是我的实体:

CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token";

HttpPost httpPost = new HttpPost(urlStr);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
params.add(new BasicNameValuePair("username", USERNAME));
params.add(new BasicNameValuePair("password", PASSWORD));

httpPost.setEntity(new UrlEncodedFormEntity(params));

CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();
CloseableHttpClient=HttpClients.createDefault();
字符串urlStr=”https://test.salesforce.com/services/oauth2/token";
HttpPost HttpPost=新的HttpPost(urlStr);
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“授权类型”、“密码”);
参数add(新的BasicNameValuePair(“客户端id”,客户端id));
参数add(新的BasicNameValuePair(“客户机密”,客户机密));
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(参数));
CloseableHttpResponse response=client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
InputStream is=response.getEntity().getContent();
字符串responsest=IOUtils.toString(即“UTF-8”);
系统输出打印LN(应答器);
client.close();

在这种情况下,我也得到了同样的回答。我的Java代码中是否遗漏了什么?如何将这两种模式结合起来,将
params
jsonPayload
作为实体?

以下是我使用Bing搜索API的经验

      URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/search");

        builder.setParameter("q", line);
        builder.setParameter("count", "10");
        builder.setParameter("offset", "0");
        builder.setParameter("mkt", "en-us");
        builder.setParameter("safesearch", "Moderate");

        URI uri = builder.build();
        HttpGet request = new HttpGet(uri);

        request.setHeader(api key);
        HttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

稍后,您需要阅读
实体
,并根据需要对其进行操作。

您是否应该使用
URIBuilder
HttpGet
?@lonesome也许这就是我应该使用的。谢谢你的提示。如果你认为有帮助的话,我可以用类似案例的代码(bing搜索API)发布一个答案。@lonesome当然,请继续。那就更好了。再次感谢。URI中没有setParameter方法
CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token";

HttpPost httpPost = new HttpPost(urlStr);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
params.add(new BasicNameValuePair("username", USERNAME));
params.add(new BasicNameValuePair("password", PASSWORD));

httpPost.setEntity(new UrlEncodedFormEntity(params));

CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();
      URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/search");

        builder.setParameter("q", line);
        builder.setParameter("count", "10");
        builder.setParameter("offset", "0");
        builder.setParameter("mkt", "en-us");
        builder.setParameter("safesearch", "Moderate");

        URI uri = builder.build();
        HttpGet request = new HttpGet(uri);

        request.setHeader(api key);
        HttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();