我正在使用套接字来创建给定服务器的POST请求。响应大部分都回来了,我正在使用编码为“UTF-8”的InputStream来读取服务器的响应。大多数响应是有道理的,我能够正确地查看HTML,但是,看似随机,我看到“1ffa”,“6e8”,“1972”,“90”,“0”等代码出现我正在读它的响应单行。这是我创建和阅读响应的方式。
String hostname = "server";
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = new Socket(hostname, 8080);
// Create streams to securely send and receive data to the server
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
PrintWriter writer = new PrintWriter(out);
writer.println("POST /handlerServlet http/1.1");
writer.println("Host: " + hostname);
String parameters="params=" + URLEncoder.encode("paramsToEncode", "UTF-8");
writer.println("Content-Length: " + parameters.length());
writer.println("Content-Type: application/x-www-form-urlencoded");
writer.println("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
writer.println("Keep-Alive: 115");
writer.println("Connection: keep-alive");
writer.println("\r\n" + parameters + "\r\n");
writer.flush();
// Read from in and write to out...
String input = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer result = new StringBuffer();
boolean startWriting = false;
FileOutputStream outStream1 = new FileOutputStream(new File("/file1.txt"));
Writer outWriter = new OutputStreamWriter(outStream1, "UTF-8");
while ( (input = reader.readLine()) != null) {
result.append(input);
outWriter.write(input + "\n");
result.append('\n');
}
System.out.println(result.toString());
outWriter.close();
// Close the socket
in.close();
有没有人知道为什么我会看到这样的人物?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="http://server:8080/HW/YX+JpCEnNDe5B87CCyFj5KR7z9rqlwRK77aMm/44221331.htm">
1ffa
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body bgcolor="#ffffff">
<!-- Created by Oracle Reports 21:14 Tue Jun 29 09:14:32 PM, 2010 -->
....
<tr valign=top>
<td height=10></td>
<td width=80 colspan=3 align=center><font size=2 face="helvetica">V002A050001</font></td>
<
1ffa
td></td>
正如您所看到的,让这些字符出现在随机位置会导致HTML代码出现一些麻烦行为。
感谢。
答案 0 :(得分:8)