复制代码
- //:POSTtest.cpp
- //CGI_vectorworksaseasilywithPOSTasit
- //doeswithGET.Writtenin"pure"C++.
- #include<iostream.h>
- #include"CGITools.h"
- voidmain(){
- cout<<"Content-type:text/plain
- "<<endl;
- //ForaCGI"POST,"theserverputsthelength
- //ofthecontentstringintheenvironment
- //variableCONTENT_LENGTH:
- char*clen=getenv("CONTENT_LENGTH");
- if(clen==0){
- cout<<"ZeroCONTENT_LENGTH"<<endl;
- return;
- }
- intlen=atoi(clen);
- char*query_str=newchar[len+1];
- cin.read(query_str,len);
- query_str[len]=\0;
- CGI_vectorquery(query_str);
- //Test:dumpallnamesandvalues
- for(inti=0;i<query.size();i++)
- cout<<"query["<<i<<"].name()=["<<
- query[i].name()<<"],"<<
- "query["<<i<<"].value()=["<<
- query[i].value()<<"]"<<endl;
- deletequery_str;//Releasestorage
- }///:~
复制代码
- <HTML>
- <HEAD>
- <METACONTENT="text/html">
- <TITLE>AtestofstandardHTMLPOST</TITLE>
- </HEAD>
- Test,usesstandardhtmlPOST
- <Formmethod="POST"ACTION="/cgi-bin/POSTtest">
- <P>Field1:<INPUTTYPE="text"NAME="Field1"
- VALUE=""size="40"></p>
- <P>Field2:<INPUTTYPE="text"NAME="Field2"
- VALUE=""size="40"></p>
- <P>Field3:<INPUTTYPE="text"NAME="Field3"
- VALUE=""size="40"></p>
- <P>Field4:<INPUTTYPE="text"NAME="Field4"
- VALUE=""size="40"></p>
- <P>Field5:<INPUTTYPE="text"NAME="Field5"
- VALUE=""size="40"></p>
- <P>Field6:<INPUTTYPE="text"NAME="Field6"
- VALUE=""size="40"></p>
- <p><inputtype="submit"name="submit"></p>
- </Form>
- </HTML>
复制代码
- //:POSTtest.java
- //AnappletthatsendsitsdataviaaCGIPOST
- importjava.awt.*;
- importjava.applet.*;
- importjava.net.*;
- importjava.io.*;
- publicclassPOSTtestextendsApplet{
- finalstaticintSIZE=10;
- Buttonsubmit=newButton("Submit");
- TextField[]t=newTextField[SIZE];
- Stringquery="";
- Labell=newLabel();
- TextAreata=newTextArea(15,60);
- publicvoidinit(){
- Panelp=newPanel();
- p.setLayout(newGridLayout(t.length+2,2));
- for(inti=0;i<t.length;i++){
- p.add(newLabel(
- "Field"+i+"",Label.RIGHT));
- p.add(t[i]=newTextField(30));
- }
- p.add(l);
- p.add(submit);
- add("North",p);
- add("South",ta);
- }
- publicbooleanaction(Eventevt,Objectarg){
- if(evt.target.equals(submit)){
- query="";
- ta.setText("");
- //Encodethequeryfromthefielddata:
- for(inti=0;i<t.length;i++)
- query+="Field"+i+"="+
- URLEncoder.encode(
- t[i].getText().trim())+
- "&";
- query+="submit=Submit";
- //SendthenameusingCGIsPOSTprocess:
- try{
- URLu=newURL(
- getDocumentBase(),"cgi-bin/POSTtest");
- URLConnectionurlc=u.openConnection();
- urlc.setDoOutput(true);
- urlc.setDoInput(true);
- urlc.setAllowUserInteraction(false);
- DataOutputStreamserver=
- newDataOutputStream(
- urlc.getOutputStream());
- //Sendthedata
- server.writeBytes(query);
- server.close();
- //Readanddisplaytheresponse.You
- //cannotuse
- //getAppletContext().showDocument(u);
- //todisplaytheresultsasaWebpage!
- DataInputStreamin=
- newDataInputStream(
- urlc.getInputStream());
- Strings;
- while((s=in.readLine())!=null){
- ta.appendText(s+"
- ");
- }
- in.close();
- }
- catch(Exceptione){
- l.setText(e.toString());
- }
- }
- elsereturnsuper.action(evt,arg);
- returntrue;
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |