Java API帖子呼叫特殊字符

发布于 2025-01-28 16:26:09 字数 6576 浏览 1 评论 0 原文

我目前使用Java使用HTTPCLIENT框架的帖子通话,该方法不是我们的,我发现了特殊字符的有趣问题。

  1. 呼叫是发送JSON对象,而不是使用URL参数。
  2. 该通话在我的代码和邮递员中很好地工作,但是,如果我在混音中放了一些特殊的角色,则httpclient失败了(但在Postman中,请保持正常工作)。

我几乎完全确定我的问题是我错过了有关编码的标题,但我不知道我要在电话中包括哪一个,我发现了很少的信息。

呼叫的示例。 (我将删除一些数据安全性)。

这是邮递员呼叫的标题:

POSTMAN HEADERS:

And this is are the Ones for my HTTPClient call:

headers={Authorization=Bearer XXXXXXXXXXXXXXXX, Accept-Encoding=gzip, deflate, br, Content- type = application/json}

,您可以看到问题,但我不知道我必须告知哪个参数,更重要的是,为什么。

PD:我之前已经检查过,但是在这里没有发现任何类似的问题。

感谢您的帮助

编辑清除率:

当我说“在混音中放一些特殊字符”时,我发送的iinside的多个变量之一是我的json对象是一个可以包含特殊字符的字符串,要更具体,我们首先发送名称和姓氏,实际问题是关于字符“ñ”。

感谢您的问题可怕。

edit2:

我的日志中的API调用:

2022-05-11 10:20:45,118错误[es.fiatc.http_client.genericoperation.genericoperation.execute] - 信息:javax.net.net.ssl.truststore = null 2022-05-11 10:20:45,495信息[es.fiatc.http_client.dao.dao.httpdao.execute] - 执行方法:使用uri发布:https:// 2022-05-11 10:20:45,498信息[es.fiatc.http_client.dao.httpdao.execute] - 参数:null 2022-05-11 10:20:45,501信息[es.fiatc.http_client.dao.dao.httpdao.execute] - 标头:{授权=携带者xxxxxxxxxxxxxxxxxxx,content-type = application/jpapplication/jpapplication/jpapplip/ 2022-05-11 10:20:45,503信息[es.fiatc.http_client.dao.httpdao.execute] - 超时= 30000 2022-05-11 10:20:45,506信息[es.fiatc.http_client.dao.httpdao.execute] - 用户:null 2022-05-11 10:20:45,508信息[es.fiatc.http_client.dao.httpdao.execute] - pwd:null 2022-05-11 10:20:45,511信息[es.fiatc.http_client.dao.httpdao.execute] - charset:utf-8 2022-05-11 10:20:45,513信息[es.fiatc.http_client.dao.httpdao.execute] - trustcertificate:null 2022-05-11 10:20:45,516信息[es.fiatc.http_client.dao.httpdao.execute] - trustcertificatepwd:null 2022-05-11 10:20:45,519信息[es.fiatc.http_client.dao.httpdao.execute] - fiatcclientcertificate:false 2022-05-11 10:20:45,521信息[es.fiatc.http_client.dao.httpdao.execute] - entity(class):java.lang.string 2022-05-11 10:20:45,524信息[es.fiatc.http_client.dao.httpdao.execute] - entitycharset:null 2022-05-11 10:20:45,526信息[es.fiatc.http_client.dao.httpdao.execute] - EntityMimeType:application/json 2022-05-11 10:20:45,529信息[es.fiatc.http_client.dao.httpdao.execute] - preemptive_authentication:false 2022-05-11 10:20:52,141 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Executing request POST XXXXXXXXXXXXXXXXXXXXXX HTTP/1.1 2022-05-11 10:20:53,755信息[es.fiatc.http_client.dao.dao.httpdao.execute] - result = {headers = {content-type = application/json/json,trassing-envers-endecoding = bucked = bucked = bucked = cack-conting = cack-coding ,Execres = THU,1981年11月19日08:52:00 GMT,Cache-Control = No-Cache,pragma = no-Cache,set-cookie = lang = es;到期=星期五,10-Jun-2022 08:21:01 GMT; max-age = 2592000;路径=/;安全,日期= 2022年5月11日星期三08:21:01格林尼治标准时间,strict-transport-security = max-age = 31536000;包括Ububdomains;预紧力,x-xss-protection = 1; mode = block,x-content-type-options = nosniff,connection = close = close},status_code = 200,conical_phrase = ok ok,content = [b@166a7559} 2022-05-11 10:20:53,756错误[es.fiatc.sld_guia_medica.dao.apidao.callapipost] - {headers = {content-type = application/json,transver-incoding =块,vary = accept-编码,expires = thu,1981年11月19日08:52:00 ggmt,cache-control = nocontrol = no-cache,pragma = no--- pragma = no--缓存,set-cookie = lang = es;到期=星期五,10-Jun-2022 08:21:01 GMT; max-age = 2592000;路径=/;安全,日期= 2022年5月11日星期三08:21:01格林尼治标准时间,strict-transport-security = max-age = 31536000;包括Ububdomains;预加载,X-xss-protection = 1; mode = block,x-content-type-options = nosniff,connection = close = close}, status_code = 200,conical_phrase = ok ok,content = [b@166a7559} 2022-05-11 10:20:54,935 INFO [es.fiatc.sld_guia_medica.tuotempo.actions.tuotempoaseguradoaction.executaions.execute] -

{“ result> {”结果“ ,“ sessionId”:null,“ execution_time”:506,“ net_execution_time”:506}

我发送的jsonObject的示例:

{“ email”:“”,“”,“ userLid”:008368790204,“ idnumber” “:“ xxxxxxx”,“ idtype”:“ 1”,“ Mobile_number”:“ 6666666666”,“隐私”:“ 1”,“ fname”:“ Maria del Carmen”,“ Lname”,“ lname”:“ Osuna”:“ Osuna”,“ GEDENDER” “:“ f”,“ date_of_birth”:“ 1969-07-02”,“ lname2”:“nuñez”,“语言”:“ es”}

如果lname2中的特殊字符消失了,API呼叫效果很好。

编辑3:

虽然我无法发布所有的爪哇电话,但这些是编码相关部分:

public static Map execute(String method, String url, Map<String, String> params, Map<String, String> headers, 
            String user, String pwd, String trustCertificate, String trustCertificateClave, String charset, 
            Integer timeout, Object entityRequest, String entityCharset, String entityMimeType, 
            boolean preemtiveAuth, boolean fiatcCertificate) throws Exception   {
        // Default values
        if (charset == null) charset = "UTF-8";
        if (timeout == null) timeout = 30000;

        // URI
        URI uri = new URI(url);
        

    // Creamos get o post
    if (StringUtils.equals(StringUtils.trim(method), "post"))
    {
        // Creamos post
        HttpPost httpPost = new HttpPost(url);

        // Insertamos params en post
        if (formparams != null)
        {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, charset);
            httpPost.setEntity(entity);                
        }
        
        // Insertamos entity en post (también multiparts,...)
        insertEntity(httpPost, null, entityRequest, entityMimeType, entityCharset);

        // Ponemos en uriRequest
        httpPost.setConfig(requestConfig);            
        uriRequest = httpPost;
    }

  private static void insertEntity(HttpPost httpPost, HttpPut httpPut, Object entityRequest, String entityMimeType, String entityCharset) throws UnsupportedEncodingException
    {
        // Miramos si existe entidad
        if (entityRequest == null) return;
        
        // Creamos contentType
        ContentType contentType = null;
        if (entityMimeType != null)
        {
            if (entityCharset == null)  contentType = ContentType.create(entityMimeType);
            else                        contentType = ContentType.create(entityMimeType, entityCharset);
        }

邮递员呼叫: ”在此处输入图像说明”

再次,对我的问题的任何格式错误感到抱歉

I currently have a POST call with HTTPClient framework using JAVA, the method is not ours and i have found an interesting problem with the special characters.

  1. The call is sending a JSON Object, not using URL parameters.
  2. The call works well in my code and in postman but, if I put some special characters in the mix, the HTTPClient fails (but in postman keep working fine).

I am almost completely sure that my problem is that I am missing some header about my encoding but I don´t know which one i have to include in my call and i found very few info about this.

Example of the calls. (I will delete a few things for Data Security).

This are the Headers for the POSTMAN Call:

POSTMAN HEADERS:

And this is are the Ones for my HTTPClient call:

headers={Authorization=Bearer XXXXXXXXXXXXXXXX, Accept-Encoding=gzip, deflate, br, Content-Type=application/json}

As you can see, i am sure the problem is here, but i don´t know which parameter i have to inform and more importantly, why.

PD: I have checked before but didn´t found any similar questions around here.

Thanks for the help

Edit for clearance:

When i say "put some special characters in the mix" One of the multiple variables that i send iinside my JSON Object is a String that can contain special characters, to be more specific, we are sending first names and last names, and the actual problem is about the character 'Ñ'.

Thanks for the question Scary Wombat.

Edit2:

The api call in my LOG:

2022-05-11 10:20:45,118 ERROR [es.fiatc.http_client.GenericOperation.execute]- INFO: javax.net.ssl.trustStore = null
2022-05-11 10:20:45,495 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Executing method:post with uri: https://XXXXXXXXXXXXXXXXXXXXXXXX
2022-05-11 10:20:45,498 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Params: null
2022-05-11 10:20:45,501 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Headers: {Authorization=Bearer XXXXXXXXXXXXXXXXX, Content-Type=application/json}
2022-05-11 10:20:45,503 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Timeout = 30000
2022-05-11 10:20:45,506 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- User: null
2022-05-11 10:20:45,508 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Pwd: null
2022-05-11 10:20:45,511 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Charset: UTF-8
2022-05-11 10:20:45,513 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- TrustCertificate: null
2022-05-11 10:20:45,516 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- TrustCertificatePwd: null
2022-05-11 10:20:45,519 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- FiatcClientCertificate: false
2022-05-11 10:20:45,521 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Entity (class): java.lang.String
2022-05-11 10:20:45,524 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- EntityCharset: null
2022-05-11 10:20:45,526 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- EntityMimeType: application/json
2022-05-11 10:20:45,529 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Preemptive_authentication: false
2022-05-11 10:20:52,141 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- Executing request POST XXXXXXXXXXXXXXXXXXXXXX HTTP/1.1
2022-05-11 10:20:53,755 INFO [es.fiatc.http_client.dao.HTTPDao.execute]- RESULT = {headers={content-type=application/json, transfer-encoding=chunked, vary=Accept-Encoding, expires=Thu, 19 Nov 1981 08:52:00 GMT, cache-control=no-cache, pragma=no-cache, set-cookie=lang=es; expires=Fri, 10-Jun-2022 08:21:01 GMT; Max-Age=2592000; path=/; secure, date=Wed, 11 May 2022 08:21:01 GMT, strict-transport-security=max-age=31536000; includeSubDomains; preload, x-xss-protection=1;mode=block, x-content-type-options=nosniff, connection=close}, status_code=200, reason_phrase=OK, content=[B@166a7559}
2022-05-11 10:20:53,756 ERROR [es.fiatc.sld_guia_medica.dao.APIDao.callAPIPost]-
{headers={content-type=application/json, transfer-encoding=chunked, vary=Accept-Encoding, expires=Thu, 19 Nov 1981 08:52:00 GMT, cache-control=no-cache, pragma=no-cache, set-cookie=lang=es; expires=Fri, 10-Jun-2022 08:21:01 GMT; Max-Age=2592000; path=/; secure, date=Wed, 11 May 2022 08:21:01 GMT, strict-transport-security=max-age=31536000; includeSubDomains; preload, x-xss-protection=1;mode=block, x-content-type-options=nosniff, connection=close}, status_code=200, reason_phrase=OK, content=[B@166a7559}
2022-05-11 10:20:54,935 INFO [es.fiatc.sld_guia_medica.tuotempo.actions.TuotempoAseguradoAction.execute]-

{"result":"ERROR","message":"A valid Userid is needed","sessionid":null,"execution_time":506,"net_execution_time":506}

An example of the JSONObject that i send:

{"email":"","userlid":"008368790204","idnumber":"XXXXXXX","idtype":"1","mobile_number":"666666666","privacy":"1","fname":"MARIA DEL CARMEN","lname":"OSUNA","gender":"F","date_of_birth":"1969-07-02","lname2":"NUÑEZ","language":"es"}

If the special character in lname2 disappear the api call works fine.

Edit 3:

while i can not post all of my java for the call these are the encoding relevant parts:

public static Map execute(String method, String url, Map<String, String> params, Map<String, String> headers, 
            String user, String pwd, String trustCertificate, String trustCertificateClave, String charset, 
            Integer timeout, Object entityRequest, String entityCharset, String entityMimeType, 
            boolean preemtiveAuth, boolean fiatcCertificate) throws Exception   {
        // Default values
        if (charset == null) charset = "UTF-8";
        if (timeout == null) timeout = 30000;

        // URI
        URI uri = new URI(url);
        

    // Creamos get o post
    if (StringUtils.equals(StringUtils.trim(method), "post"))
    {
        // Creamos post
        HttpPost httpPost = new HttpPost(url);

        // Insertamos params en post
        if (formparams != null)
        {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, charset);
            httpPost.setEntity(entity);                
        }
        
        // Insertamos entity en post (también multiparts,...)
        insertEntity(httpPost, null, entityRequest, entityMimeType, entityCharset);

        // Ponemos en uriRequest
        httpPost.setConfig(requestConfig);            
        uriRequest = httpPost;
    }

  private static void insertEntity(HttpPost httpPost, HttpPut httpPut, Object entityRequest, String entityMimeType, String entityCharset) throws UnsupportedEncodingException
    {
        // Miramos si existe entidad
        if (entityRequest == null) return;
        
        // Creamos contentType
        ContentType contentType = null;
        if (entityMimeType != null)
        {
            if (entityCharset == null)  contentType = ContentType.create(entityMimeType);
            else                        contentType = ContentType.create(entityMimeType, entityCharset);
        }

The postman call: enter image description here

Again, sorry for any format error in the way i make my question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2025-02-04 16:26:09

最后我解决了它。

问题在我们的框架中很深,但是一旦发现这是一个简单的修复:

当您将实体作为帖子通话发送并且具有特殊字符时,通常会编码它。正如Ewramner建议我检查的那样,我确认我们的实体编码良好,但目前将实体发送到API一样,我们丢失了Charset配置(不是全局的配置,而是实体),

解决方案是添加特定的特定实体类型中的实体的字符集:

data.put("entity", paramsCrear.toString());
data.put("entity_mime_type", "application/json");
data.put("entity_charset", "UTF-8");

邮递员似乎自动将此实体_charset添加到您所做的每个表单数据中,这就是为什么我在寻找这个特定点的问题。

非常感谢@ewramner和@scary_wombat

以及所有人,请记住,如果您使用实体中的Java中使用帖子呼叫,则可能需要在WICH中指定您对实体进行编码的charset(如果将其放入标题中,则也。

Finally i solved it.

The problem was deep in our framework but once spotted it was an easy fix:

When you send an Entity as a Post Call and it have special characters, yo usually encode it. Just as ewramner suggested i checked and i confirmed that we had the entity well encoded BUT at the moment to send the entity to the API, we lost the Charset configuration (not the global one but the entity one)

The solution was to add the specific charset of the entity inside the entity type like this:

data.put("entity", paramsCrear.toString());
data.put("entity_mime_type", "application/json");
data.put("entity_charset", "UTF-8");

It seems that postman automatically add this entity_charset to every form-data that you make that´s why I had problems looking for this specific point.

Thank you so much @ewramner and @Scary_wombat

And everyone, keep in mind that if you use a post call in java with entity, you might need to specify the charset in wich you encode the entity (event if it´s put in the headers too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文