简体   繁体   English

在Java请求中使用$ filter参数以动态CRM时的400状态代码

[英]400 status code when using $filter parameter in java request to dynamics crm

I'm trying query data from dynamics CRM through java HttpUrlConnection object. 我正在尝试通过java HttpUrlConnection对象从Dynamics CRM查询数据。 I always get an HTTP 400 status code when my request include the $filter parameter. 当我的请求包含$ filter参数时,我总是得到HTTP 400状态代码。 But when I remove this parameter my request works fine. 但是,当我删除此参数时,我的请求运行正常。 I have tried my request directly on a browser and it works fine too. 我已经在浏览器上直接尝试了我的请求,它也很好用。 The code below shows how I built my request 下面的代码显示了我如何构建请求

public String getContactByPhoneNumber(String phoneNumber) throws Exception {
    HttpURLConnection connection = null;
    URL url = new URL(completeURL + "/contacts?select=lastname&$filter=telephone1 eq '"+phoneNumber+"'");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("OData-MaxVersion", "4.0");
    connection.setRequestProperty("OData-Version", "4.0");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Prefer", "odata.include-annotations=\"*\"");
    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    connection.addRequestProperty("Authorization", "Bearer " + token);
    if (connection.getResponseCode() == 200) {
        String response = getResponseAsString(connection);
        return response;
    } else {
        String error = getErrorAsString(connection);
        System.err.println(error);
        return null;
    }

}

Did anyone met this error? 有没有人遇到这个错误? How can I solve it? 我该如何解决?

I haven't tested this myself, so I can't guarantee that it works, but you could try URL-encoding the $filter parameter name and its value: 我没有亲自测试过,因此无法保证它能正常工作,但是您可以尝试对$filter参数名称及其值进行URL编码:

import java.net.URLEncoder;

// ... 

  URL url = new URL(
    completeURL + "/contacts?select=lastname&"
    + URLEncoder.encode("$filter", "UTF-8") + "="
    + URLEncoder.encode("telephone1 eq '"+phoneNumber+"'", "UTF-8"));

Luke mentioned you had misspelled "filter" in your original text. 卢克(Luke)提到您在原始文本中拼写了“过滤器”。 That may have something to do with it if it was not just a typo here in your post. 如果这不仅仅是您帖子中的错字,可能与它有关。

Also, "$" is a reserved character in URI syntax according to RFC 2396. http://www.faqs.org/rfcs/rfc2396.html 另外,“ $”是根据RFC 2396的URI语法中的保留字符。http://www.faqs.org/rfcs/rfc2396.html

2.2. Reserved Characters

 Many URI include components consisting of or delimited by, certain
special characters.  These characters are called "reserved", since
their usage within the URI component is limited to their reserved
purpose.  If the data for a URI component would conflict with the
reserved purpose, then the conflicting data must be escaped before
forming the URI.

  reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                "$" | ","

The "reserved" syntax class above refers to those characters that are
allowed within a URI, but which may not be allowed within a
particular component of the generic URI syntax; they are used as
delimiters of the components described in Section 3.

Characters in the "reserved" set are not reserved in all contexts.
The set of characters actually reserved within any given URI
component is defined by that component. In general, a character is
reserved if the semantics of the URI changes if the character is
replaced with its escaped US-ASCII encoding.

You could try to form the URL without it and see if that works. 您可以尝试在没有URL的情况下形成URL,然后查看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM