16 83访问cgi 从小程序中运行CG程序(post) post方法通过标准输出向cg传送参数(适合参数 较多的情况) 用post方法要有数据输出流 Socketruncgi-newsOcket(www.jamsa.com,80); 建立 socket连接 Data Outputstream ds-runcgi. getOutputstreamo 建立输入流 ds.printin(“POST/cgi-bin/ guestbook. pl”); 运行cg程序
16 8.3 访问cgi 从小程序中运行CGI程序(post) post方法通过标准输出向cgi传送参数(适合参数 较多的情况) 用post方法要有数据输出流 Socket runcgi=new Socket(“www.jamsa.com”,80); 建立socket连接 DataOutputStream ds=runcgi.getOutputStream(); 建立输入流 ds.println(“POST/cgi-bin/guestbook.pl”); 运行cgi程序
83访问cgi 通知发送的数据类型 ds.printIn(“ Content-type: plain/text”): 通知发送的数据长度 String data=“ Hello world” ds. printIn(“ Content- length”+ data length+”Ⅶn”); 发送数据 ds printIn(data)
17 8.3 访问cgi 通知发送的数据类型 ds.println(“Content-type:plain/text”); 通知发送的数据长度 String data=“Hello world” ds.println(“Content-length”+data.length+”\n”); 发送数据 ds.println(data);
18 83访问cgi cgi接收post方法送来的数据就像从标准输入读 数据一样 getchar;gets0;:(C语言的情况)(要建立数据流) cg返回数据时要指明数据类型等信息: printf(“ Content-type:text/ plain nn”) 或 printf“ Content-type:text/ hominin”); 或 printf(“ Content-type: Image/ gif\nn”); cg定服务器与外部程序交换信息的协议
18 8.3 访问cgi cgi接收post方法送来的数据,就像从标准输入读 数据一样. getchar();gets();(C语言的情况)(要建立数据流) cgi返回数据时要指明数据类型等信息: printf(“Content-type:text/plain\n\n”); 或 printf(“Content-type:text/html\n\n”); 或 printf(“Content-type:image/gif\n\n”); cgi规定服务器与外部程序交换信息的协议
访问cgi 19 客户端 URL连接 服务器端 java程序 DataInputstream cgi程序 返回结果 String count srt URL renew UrlchTtp: //202.118.239.38/cgi-bin/count. cgi); DataInputstream data=new DataInputStream(url. open Streano) count str= data. headline;(此处读入的是cg序的输出) g drawstring( You are the“+ count str+” visitor”)
19 8.3 访问cgi String count_srt URL url=new URL(“http://202.118.239.38/cgi-bin/count.cgi”); DataInputStream data=new DataInputStream(url.openStrean()); count_str=data.readLine();(此处读入的是cgi程序的输出) g.drawstring(“You are the “+count_str+ ”visitor”); 返回结果 DataInputStream 客户端 java程序 服务器端 cgi程序 URL连接
20 84URL连接 URL双向通信( URLConection) URLConnection是一个一http为中心的类 1.建立连接 URL reNew UrlchTtp:/www.yahoocom/); URLConnection con=urL. open Connection (; 2.向服务器端送数据 Printstream outstreamEnew Printstream(con. getOutputStreamo); outstream printIn(string data)
20 8.4 URL连接 URL双向通信(URLConection) URLConnection是一个一http为中心的类 1. 建立连接 URL url=new URL(“http://www.yahoo.com/”); URLConnection con=url.openConnection(); 2. 向服务器端送数据 PrintStream outStream=new PrintStream(con.getOutputStream()); outStream.println(string_data);