一个Servlet如下:
public class RequestHeaderExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain; charset=SJIS");
//response.set
response.setHeader("ReturnCode", "0");
response.setHeader("OP_HOS", null);
response.setHeader("OP_OPCD", "日本");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
一个应用程序要访问这个Servlet,得到"OP_OPCD"的值,为何我得到的是乱码,转换也不行.
应用程序部分代码如下:
url = new URL(serviceUrl);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
// Set relative property
huc.setRequestMethod("POST");
huc.setRequestProperty("Content-Type", "text/plain; charset=SJIS");
String test = huc.getHeaderField("OP_LICCD");
//test = new String(test.getBytes(SJIS));
System.out.println("test result : " + test);
此处结果为乱码,如果该字段的值为英文则,无乱码。这是为什么?
public class RequestHeaderExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain; charset=SJIS");
//response.set
response.setHeader("ReturnCode", "0");
response.setHeader("OP_HOS", null);
response.setHeader("OP_OPCD", "日本");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
一个应用程序要访问这个Servlet,得到"OP_OPCD"的值,为何我得到的是乱码,转换也不行.
应用程序部分代码如下:
url = new URL(serviceUrl);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
// Set relative property
huc.setRequestMethod("POST");
huc.setRequestProperty("Content-Type", "text/plain; charset=SJIS");
String test = huc.getHeaderField("OP_LICCD");
//test = new String(test.getBytes(SJIS));
System.out.println("test result : " + test);
此处结果为乱码,如果该字段的值为英文则,无乱码。这是为什么?
改成GBK也不行
----------------------------------------------------------
response.setContentType("text/html;charset=gbk");