问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用java写了一个Http client,但向服务器post的时候传中文参数老是乱码,请大侠明示一下

发布网友 发布时间:2022-04-30 00:05

我来回答

3个回答

热心网友 时间:2023-10-10 21:01

以上的2个方法最好都要用上 过滤器只能解决POST请求 ,要处理GET请求就要用
bytes = string.getBytes("iso-8859-1") 得到原始的字节串,再用 string = new String(bytes, "GB2312") 重新得到正确的字符串 。
这个方法,所以最好2个都要写,这样不管是POST还是GET请求就都能解决了。

热心网友 时间:2023-10-10 21:01

写个过滤器就行了。将编码格式写在web.xml的配置文件里。
下面的这个过滤器是tomcat提供的。
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;

/**
* <p>Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
*/

public class SetCharacterEncodingFilter implements Filter {

// ----------------------------------------------------- Instance Variables

/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

// --------------------------------------------------------- Public Methods

/**
* Take this filter out of service.
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}

/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

// Pass control on to the next filter
chain.doFilter(request, response);

}

/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;

}

// ------------------------------------------------------ Protected Methods

/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}

热心网友 时间:2023-10-10 21:02

bytes = string.getBytes("iso-8859-1") 得到原始的字节串,再用 string = new String(bytes, "GB2312") 重新得到正确的字符串 。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
苹果电脑电池充不进电苹果电脑充不进去电是怎么回事 苹果电脑不充电没反应苹果电脑充电指示灯不亮充不了电怎么办 狗狗更加忠诚护家、善解人意,养一只宠物陪伴自己,泰迪能长多大... 描写泰迪狗的外形和特点的句子 国外留学有用吗 花钱出国留学有用吗 !这叫什么号 百万医疗赔付后是否可以续保 前一年理赔过医疗险还能续保吗? 医疗住院险理赔后还能购买吗? 客户端java程序收到服务器端HTTP 200 OK 返回的json ,怎么在客户端的Eclisp上打印出来 如何用Java客户端/applet通过HTTP POST上传文件 java 客户端到服务器的工作原理 JAVA HttpServlet 里面实现了一个简单的doGet,发送会客户端的响应,总被截短 请教各位问题:java web客户端上传图片到服务器的D盘下,请问客户端怎么通过http访问图片 websocket java 怎么做服务端和客户端 请问如何写一个java 服务器端的servlet,然后可以当客户端发出http请求的时候,可以接受请求,读取数据库 Java服务端开发,客户端http请求,请问服务端如何处理,可以使客户端的response的statuscode是401 Java Socket实现HTTP客户端来理解Session和Cookie的区别和联系_百度知 ... java http 客户端有哪些 买房子分期付款计算利息方面是如何计算的 锦州火车站到小岭子机场怎么走?打车多少钱?我是在去哪儿网买的机票,登机具体流程和飞机上有哪些服务呢 小岭子机场到大连理工盘锦校区 从锦州火车站到小岭子机场怎么走? 锦州火车站到小岭子机场怎么走 锦州小岭子机场怎么样 锦州有几个机场 锦州有几个机场? vivox5pro私密短信怎么隐藏 锦州小岭子机场是不是锦州机场 无视Https证书是不是正确的Java Http Client 夏天戴黑色鸭舌帽防晒好还是白色鸭舌帽防晒好?那种好搭配衣服? 白色棒球帽和黑色棒球帽哪个比较好看?买帆布还是皮的?求意见,我是个没主见的人。。。对了,最好还说下 夏天带黑色帽子防紫外线还是白色帽子呢?哪个颜色防紫外线防晒好? 如何让暖壶胆不容易碎?是不是暖壶壳下面不要拧的太紧? 暖壶底下拧不紧会影响保温吗? 暖壶底部是不是拧得越紧越好 暖瓶塞塞得太紧,暖瓶会不会爆炸? 刚打热水,暖壶盖盖太紧会不会爆炸啊? 暖壶的盖子盖紧了为什么会弹起来?(原理是什么) 大桂花树移栽要注意什么 桂花树可以插枝栽培吗,桂花树移栽要注意什么 桂花树怎么样正确移植 工程08定额是什么? 08定额是什么 08定额,四类取费与一类取费的区别? liz lisa衣服价位,上海的 03定额与08定额主要有什么不同 liz lisa是什么 河南的02定额与08定额的区别有哪些?