java Http请求server数据

java Http请求server数据

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

怪蜀黍
public class Client {

 /**
  * @param args
  */
 public static void main(String[] args) {
  try{
  //开启socket http请求
  URL url=new URL("http://localhost:8080/XMLTransferDemoServer/servlet/ServerServlet");
  //Socket clientSocket=new Socket(url.getHost(),url.getPort());
  URLConnection uc = url.openConnection();
  uc.setUseCaches(false);
  uc.connect();
  //OutputStream os = clientSocket.getOutputStream();
  InputStream is = uc.getInputStream();
  DataInputStream dis=new DataInputStream(uc.getInputStream());
  //inputStreamReadLen=dis.read(byteInputStreamData);
  //读取服务器xml数据缓冲区
  StringBuffer tmpSB=new StringBuffer();
  String str="";
  while((str=dis.readLine())!=null)
  {
   tmpSB.append(str);
  }
  
  is.close();
  dis.close();
  
  //调用xml数据转换逻辑转换读取的数据
  String xmlAnStr=anXMLData(tmpSB.toString());
  //将转换后的xml数据存储到database
  System.out.println(xmlAnStr);

风之境地
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 //自己实现转换逻辑
 public static String anXMLData(String xml)
 {
  return xml;
 }
}

你可能感兴趣的:(java Http请求server数据)