// 创建 模拟HTTP请求
HttpClient httpClient = new DefaultHttpClient();
// 设置超时 8秒
httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
8000); // 超时设置
httpClient.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 10000);// 连接超时
httpClient.getParams().setIntParameter(
HttpConnectionParams.SOCKET_BUFFER_SIZE, 20 * 1024);// 缓冲区
// 设置连接主机 和 端口
HttpHost httpHost = new HttpHost(hp.getHostString(), Integer
.parseInt(hp.getPortString())); // 设置需要请求的 selvet 地址(注 主机后面的
// 如:"/XMMyGoWeb/servlet/MyGoServer.HttpPool.HttpHandlerServlet")
HttpPost httpPost = new HttpPost(hp.getServletString());
// httpPost.getParams().setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
// 20 * 1024);
// 以下这条如果不加会发现无论你设置Accept-Charset为gbk还是utf-8,
// 他都会默认返回gb2312(本例针对google.cn来说)
httpPost.setHeader("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2)");
// 用逗号分隔显示可以同时接受多种编码
httpPost.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
httpPost.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); // 设置cookie的兼容性,这一行必须要加,否则服务器无法获取登陆状态
HttpClientParams.setCookiePolicy(httpClient.getParams(),
CookiePolicy.BROWSER_COMPATIBILITY);
// // 设置参数的集合
// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//
if (cookie != null) {
// // 设置参数,格式是name:value
// nvps.add(new BasicNameValuePair("cookie",
// cookie.toString()));
// httpClient.getParams().setParameter("cookie", cookie.toString());
httpPost.setHeader("cookie", cookie.toString());
}
// nvps.add(new BasicNameValuePair("version",
// "22-Jun-2001/11:30:00-CST")); // httpClient.getParams().setParameter("version",
// "22-Jun-2001/11:30:00-CST");
// // 对参数实体进行格式转换
// UrlEncodedFormEntity urlEntity = new
// UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httpPost.setHeader("version", "22-Jun-2001/11:30:00-CST"); // 设置实体
InputStreamEntity reqEntity = new InputStreamEntity(
new ByteArrayInputStream(hp.getPc().serialize()), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
httpPost.setEntity(reqEntity);
// Execute HTTP request
HttpResponse response = null;
try {
response = httpClient.execute(httpHost, httpPost);
// 获取执行状态
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// 获取cookie
String sc = response.getFirstHeader("set-cookie").toString();
if (sc != null) {
if (hp.getIndex() == -1) {
hp.InsertCookie(hostAndProt, sc);
} else {
hp.getCookieVector().setElementAt(sc, hp.getIndex());
}
}
// 获取实体
HttpEntity entity = response.getEntity();
if (entity != null && entity.getContentLength() != 0) {
return entity;
} else {
System.out.println("httpEntity实体为空");
}
} else {
// 打印状态
System.out.println("httpClient执行状态" + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("服务器连接失败!");
return null;
} finally {
// 关闭httpClient
httpClient.getConnectionManager().shutdown();
}
return null;传值过来。~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ try { DataInputStream dis = new DataInputStream(new BufferedInputStream(
entity.getContent()));
byte[] paramArray = null; // 先判断是否有异常
int errFlag = dis.readInt(); // 有异常
if (errFlag != 1) {
// System.out.println("异常信息:" + dis.readUTF());
return null;
} else {
int recCmdType = dis.readInt();
// 如recCmdType > 0,表示是有效的命令
if (recCmdType > 0) {
int length = dis.readInt();
// 如length为0,表示没有发送过来的参数内容;
// 只有当length > 0时,才表示有发送过来的参数内容.
if (length > 0) {
paramArray = new byte[length];
int readLen = 0;
while (readLen < length) {
int readInt = dis.read();
System.out.println(readLen);
if (readInt == -1) {
break;
}
Integer A = new Integer(readInt);
byte readByte = A.byteValue();
paramArray[readLen] = readByte;
readLen++;
}
if (readLen != length) {
throw new IOException("预计发送的长度与接收到的实际长度不符.");
}
} else {
return null;
}
} else {
recCmdType = -1;
}
}报错。~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~0
1
2
.
.
.
9899
9900
9901
9902
9903
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:130)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:127)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:161)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:159)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at java.io.FilterInputStream.read(FilterInputStream.java:66)
at com.mygo.mybus.biz.impl.ProcessorContentBizImpl.doDispatchter(ProcessorContentBizImpl.java:48)
at com.mygo.mybus.biz.impl.BusBizImpl.toDispose(BusBizImpl.java:58)
at com.mygo.mybus.biz.impl.BusBizImpl.getTransferByStationStarAndEnd(BusBizImpl.java:110)
at com.mygo.mybus.servlet.BusHC.theThree(BusHC.java:269)
at com.mygo.mybus.servlet.BusHC.doPost(BusHC.java:108)
at com.mygo.mybus.servlet.BusHC.doGet(BusHC.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.mygo.mybus.filter.MyBusFilter.doFilter(MyBusFilter.java:111)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
 只要长度超过 9903字节 就报错。 我定义了20*1024的长度。想问下大家httpclient是不是有 最高缓冲区 限制。 
 请大家帮忙 如果有最高限制,如果我需要的缓冲 要 3万字节,要怎么解决呢?我需要的是只要能正常读取这3万字
 节成功反序列就解决了。
 请提供一些方案或者思路、建议。万分感谢!