Java API帖子呼叫特殊字符
我目前使用Java使用HTTPCLIENT框架的帖子通话,该方法不是我们的,我发现了特殊字符的有趣问题。
- 呼叫是发送JSON对象,而不是使用URL参数。
- 该通话在我的代码和邮递员中很好地工作,但是,如果我在混音中放了一些特殊的角色,则httpclient失败了(但在Postman中,请保持正常工作)。
我几乎完全确定我的问题是我错过了有关编码的标题,但我不知道我要在电话中包括哪一个,我发现了很少的信息。
呼叫的示例。 (我将删除一些数据安全性)。
这是邮递员呼叫的标题:
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);
}
再次,对我的问题的任何格式错误感到抱歉
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我解决了它。
问题在我们的框架中很深,但是一旦发现这是一个简单的修复:
当您将实体作为帖子通话发送并且具有特殊字符时,通常会编码它。正如Ewramner建议我检查的那样,我确认我们的实体编码良好,但目前将实体发送到API一样,我们丢失了Charset配置(不是全局的配置,而是实体),
解决方案是添加特定的特定实体类型中的实体的字符集:
邮递员似乎自动将此实体_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:
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.