日期:2014-05-20  浏览次数:20818 次

请教Socket模拟http连接走cmwap通道连接远程服务器的问题
请教一下各位,有没有人做过socket模拟http连接走cmwap连接远程服务器的问题。程序已经经过了安全签名。

主要代码如下:

SocketConnection socket = (SocketConnection) Connector
  .open("socket://" + "10.0.0.172:80");// 使用代理连接
  DataOutputStream dos = new DataOutputStream(socket
  .openOutputStream());
  DataInputStream dis = new DataInputStream(socket.openInputStream());

  // 发送数据

//封装实际要发送的消息块
  StringBuffer bodyBuffer = new StringBuffer();
  bodyBuffer.append("<C_MOST_NEW_MESSAGE>");
  bodyBuffer.append("\r\n");

//封装http协议头消息块
  StringBuffer headBuffer = new StringBuffer();
  headBuffer
  .append("POST http://remoteServerAddress:port HTTP/1.0\r\n");
  headBuffer.append("Connection: close\r\n");
  headBuffer.append("accept:*/*\r\n");
  headBuffer
  .append("Content-type: application/x-www-form-urlencoded\r\n");
  headBuffer.append("Content-length: "
  + (bodyBuffer.toString().getBytes().length) + "\r\n");
  headBuffer.append("\r\n");

//发送http头信息
  dos.write(headBuffer.toString().getBytes());

//发送实际的消息体
  dos.write(bodyBuffer.toString().getBytes());
  dos.flush();

//发送完毕,等待接受回应消息
  printResponseMessage(dis);//解析收到的回应消息
  dos.close();
  dis.close();
  socket.close();

问题的现象是:发送消息完毕之后,要等待很久才能收到cmwap回应,responseCode: 502 或者是504

远程服务器绝对没有问题,普通的http连接通过。用socket模拟http就不行,初步估计的原因是socket模拟http通过cmwap时是否需要特殊的http头设置。

需要做过这个东东的帮忙一些,多谢各位了。



------解决方案--------------------
恩,你自己写的http头不对:

headBuffer.append("POST " + url + " HTTP/1.1").append("\r\n");
headBuffer.append("Connection: Keep-Alive").append("\r\n");
headBuffer.append("Host: " + "10.0.0.172:80").append("\r\n");
headBuffer.append("User-Agent: Nokia3250/2.0 (04.14) SymbianOS/9.1 Series60/3.0 Profile/MIDP-
2.0 Configuration/CLDC-1.1").append("\r\n");//这个UA是我截获的3250的ua,具体
//自己来写
headBuffer.append("Content-Type: text/html").append("\r\n");
headBuffer.append("Accept: */*").append("\r\n");
headBuffer.append("x-wap-profile: http://uaprof.uni-wise.com/uaprof/UT/UT-T80.xml").
append("\r\n");

这样就差不多了
------解决方案--------------------
楼主的问题是没有设置HOST,代理需要知道你把信息发送到主机。
有关长连接:
http是可以保持长连接的,设置Keep-Alive即可,服务器检测到这个http报头信息,就可以不断开tcp链接,这样避免了多次建立tcp链接的耗时。上面有些专家对这个问题的回答过于草率了。