java中HTTP通讯

来源:互联网 发布:php专科考试题及答案 编辑:程序博客网 时间:2024/05/17 06:58

所谓socket通常也称作"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求。ServerSocket用于服务器端,Socket是建立网络连接时使用的。在连接成功时,应用程序两端都会产生一个Socket实例,操作这个实例,完成所需的会话。

   下面通过实例了解Socket的作用。

     首先展示实现的效果:

   

     

  在通讯中服务器先启动,本例子引用图灵机器人API。

public class Myserver extends JFrame implements ActionListener{


JTextArea jta;
JTextField jtf;
JLabel jl;
JButton jb;
JPanel jp;
ServerSocket ss;
Socket socket;
Scanner sc;
PrintWriter pw;
public Myserver(){
jta=new JTextArea();
jtf=new JTextField(15);
jb=new JButton("发送");

jp=new JPanel();

jl=new JLabel("自动回复");

jp.add(jl);
/*jp.add(jtf);
jp.add(jb);*/

JScrollPane jsp=new JScrollPane(jta);
   this.add(jsp,BorderLayout.CENTER);
   this.add(jp,BorderLayout.SOUTH);
   
   jb.addActionListener(this);
   jtf.addActionListener(this);
   
   this.setTitle("服务器");
   this.setSize(300,400);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setVisible(true);
   

try {
   
ss=new ServerSocket(9000);
System.out.println("等待连接");
socket=ss.accept();
System.out.println("已连接");
sc=new Scanner(socket.getInputStream());
pw=new PrintWriter(socket.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JButton jb;
while(true){
String str=sc.nextLine();
sentquestion(str);
jta.append("客户端:"+str+"\r\n");
try {
     if(js.has("url")){
    pw.println(js.getString("text")+"  "+js.getString("url"));
jta.append("图灵小机器人:"+js.getString("text")+"\r\n"+js.getString("url")+"\r\n");
jtf.setText("");
     }else{
     pw.println(js.getString("text"));
jta.append("图灵小机器人:"+js.getString("text")+"\r\n");
jtf.setText("");
     }
   
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}



}

private void sentquestion(String str) {
// TODO Auto-generated method stub
String question=str;
 String INFO;
try {
INFO = URLEncoder.encode(question, "utf-8");
 answerQuestion(INFO);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();

     
}
JSONObject js;
private void answerQuestion(String iNFO) {
// TODO Auto-generated method stub
String APIKEY = "图灵机器人API"; 
String getURL = "http://www.tuling123.com/openapi/api?key=" +APIKEY +"&info=" + iNFO;
String result;
try {
result = HttpUtils.doGet(getURL);
/*System.out.println(result);*/
   js=new JSONObject(result);
  
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void main(String[] args){
new Myserver();
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/* if(e.getSource()==jb||e.getSource()==jtf){
pw.println(jtf.getText());
jta.append("服务器说"+jtf.getText()+"\r\n");
jtf.setText("");
}*/

}



}

客户端代码

public class MyClient extends JFrame implements ActionListener{



JTextArea jta;
JTextField jtf;
JButton jb;
JPanel jp;
 
Socket socket;
Scanner sc;
PrintWriter pw;
 
public MyClient(){
jta=new JTextArea();
jtf=new JTextField(15);
jb=new JButton("发送");
jp=new JPanel();
 
jp.add(jtf);
jp.add(jb);
 
JScrollPane jsp=new JScrollPane(jta);
this.add(jsp,BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
 
jb.addActionListener(this);
jtf.addActionListener(this);
 
this.setTitle("客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,400);
this.setVisible(true);
 
try {
socket=new Socket("127.0.0.1",9000);

sc=new Scanner(socket.getInputStream());

pw=new PrintWriter(socket.getOutputStream(),true);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
while(true){
String str=sc.nextLine();
 
jta.append("图灵机器人:"+str+"\r\n");
 
}
}
 
public static void main(String[] args) {
// TODO Auto-generated method stub
  new MyClient();
}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb||e.getSource()==jtf){
pw.println(jtf.getText());

jta.append("会飞的鱼:"+jtf.getText()+"\r\n");

jtf.setText("");
}
}


}

0 0