用Socket类实现HTTP协议客户端应

2019-06-19 20:25:27 来源: 武汉信息港

  protected void openServer(String host,int port) throws

  UnknownHostException,IOException {

  ear();

  responseMessage=""; responseCode=-1;

  try {

  if(client!=null) closeServer();

  if(byteStream != null) {

  ose(); byteStream=null;

  }

  InetAddress address = tByName(host);

  client = new Socket(address,port==-1?80:port);

  sender = new BufferedOutputStream(tOutputStream());

  receiver = new BufferedInputStream(tInputStream());

  }catch(UnknownHostException u) {

  throw u;

  }catch(IOException i) {

  throw i;

  }

  }

  /* 关闭与Web服务器的连接 */

  protected void closeServer() throws IOException {

  if(client==null) return;

  try {

  ose(); ose(); ose();

  }catch(IOException i) {

  throw i;

  }

  client=null; sender=null; receiver=null;

  }

  protected String getURLFormat(URL target) {

  String spec = "http://"+tHost();

  if(tPort()!=-1)

  spec+=":"+tPort();

  return spec+=tFile();

  }

  /* 向Web服务器传送数据 */

  protected void sendMessage(String data) throws IOException{

  ite(tBytes(),0,ngth());

  ush();

  }

  /* 接收来自Web服务器的数据 */

  protected void receiveMessage() throws IOException{

  byte data[] = new byte[1024];

  int count=0;

  int word=-1;

  // 解析行

  while( (word=ad())!=-1 ) {

  if(word== ||word== ) {

  word=ad();

  if(word== ) word=ad();

  break;

  }

  if(count == ngth) data = addCapacity(data);

  data[count++]=(byte)word;

  }

  String message = new String(data,0,count);

  int mark = dexOf(32);

  serverVersion = bstring(0,mark);

  while( ngth() arAt(mark+1)==32 ) mark++;

  responseCode = rseInt(bstring(mark+1,mark+=4));

  responseMessage = bstring(mark,ngth()).trim();

  // 应答状态码和处理请读者添加

  switch(responseCode) {

  case 400:

  throw new IOException("错误请求");

  case 404:

  throw new FileNotFoundException( getURLFormat(target) );

  case 503:

  throw new IOException("服务器不可用" );

  }

  if(word==-1) throw new ProtocolException("信息接收异常终止");

  int symbol=-1;

  count=0;

  // 解析元信息

  while( word!= word!= word-1) {

  if(word== ) word=32;

  if(count==ngth) data = addCapacity(data);

  data[count++] = (byte)word;

  parseLine: {

  while( (symbol=ad()) -1 ) {

  switch(symbol) {

  case :

  symbol=32; break;

  case :

  case :

  word = ad();

  if( symbol== word== ) {

  word=ad();

  if(word== ) word=ad();

  }

  if( word== || word== || word32) break parseLine;

  symbol=32; break;

  }

  if(count==ngth) data = addCapacity(data);

  data[count++] = (byte)symbol;

  }

  word=-1;

  }

  message = new String(data,0,count);

  mark = dexOf(:);

  String key = null;

  if(mark0) key = bstring(0,mark);

  mark++;

  while( ngth() arAt(mark)=32 ) mark++;

  String value = bstring(mark,ngth() );

  t(key,value);

  count=0;

  }

  // 获得正文数据

  while( (word=ad())!=-1) {

  if(count == ngth) data = addCapacity(data);

  data[count++] = (byte)word;

  }

  if(count0) byteStream = new ByteArrayInputStream(data,0,count);

  data=null;

  closeServer();

  }

  public String getResponseMessage() {

  return responseMessage;

  }

  public int getResponseCode() {

  return responseCode;

  }

  public String getServerVersion() {

  return serverVersion;

  }

  public InputStream getInputStream() {

  return byteStream;

  }

  public synchronized String getHeaderKey(int i) {

  if(i=ze()) return null;

  Enumeration enum = opertyNames();

  String key = null;

  for(int j=0; j=i; j++)

  key = (String)xtElement();

  return key;

  }

  public synchronized String getHeaderValue(int i) {

  if(i=ze()) return null;

  return tProperty(getHeaderKey(i));

  }

  public synchronized String getHeaderValue(String key) {

  return tProperty(key);

  }

  protected String getBaseHeads() {

  String inf = "User-Agent: myselfHttp/1.0 "+

  "Accept: www/source; text/html; image/gif; */* ";

  return inf;

  }

  private byte[] addCapacity(byte rece[]){

  byte temp[] = new byte[ngth+1024];

  raycopy(rece,0,temp,0,ngth);

  return temp;

  }}

  注: 程序中只实现GET、HEAD、POST三种方法。其他几种因不常使用,暂且忽略。

  查看本文来源

孩子不爱吃饭怎么办
孩子不爱吃饭的4大原因
儿童消化不良吃什么药
本文标签: