Smart CODE | |
生成済みコードに関するオンラインガイド
|
C |
#include "URL.h" typedef int (*SendHandlerProc) ( URLConnection *,sc_stdcs_t *) URLConnection * uc; sc_stdcs_t * data; |
---|---|
C++ |
#include "URL.h" typedef int (*SendHandlerProc)( URLConnection *, sc_stdcs_c*) URLConnection* uc; sc_stdcs_c * data; |
Java |
public class HANDLERNAME_c extends SCOutputDataHandler { public void doit( SCStdCS csdata, OutputStream ostream) {} public static HANDLERNAME_c getNew_HANDLERNAME_c() { return new HANDLERNAME_c(); } } |
Web ブラウザは、FORM 要素を HTML 書式で取得し、テキストをコード化されたフォーマットに変換し、そのデータを<名前>=<値>の組み合わせとして送信します。<名前>は FORM 要素の名前タグに対応し、<値>はその設定およびデータに対応します。
Web ブラウザは、送信元、フォームの URL、およびその他の詳細を示すために、サーバーにコンテンツヘッダーも送信します。サーバーの中にはこの情報に注意を払い、たとえば、Netscape と Internet Explorer とでは異なる方法で応答するものもあります。高機能のサーバープログラムでは、認識できないクライアントに対しては応答を拒否する場合もあります。
デフォルトの HTML 書式インタフェースがサーバーと対話する方法を確認するには、接続をモニターするのが一番簡単です。次の
java プログラムでは、送信された内容をエコーするだけの単純なサーバープログラムを実行します。フォームの「サブミット」ボタンを押したときに送信される内容を検出するには、次の処理を行います。
次に、Java のモニタプログラムのコードを示します。これを Server.java という名前のファイルに保存し、コンパイルして実行します。
import java.net.*; import java.io.*; public class Server extends Thread { ServerSocket ss = null; public Server() { try { ss = new ServerSocket(2000); } catch (Exception e) { e.printStackTrace(); } System.out.println("Listening on port 2000"); this.start(); } public void run() { try { while(true) { Socket client = ss.accept(); handleClient( client); } } catch (IOException e) { System.out.println("Exception while listening for connection"); } } void handleClient( Socket s) throws IOException { DataInputStream in = new DataInputStream(s.getInputStream()); while (true) { String line = in.readLine(); if (line == null) return; System.out.println( line); } } public static void main(String []args) { Server s = new Server(); } }
要求ヘッダーを変更する | ||
---|---|---|
言語 | 使用法 | |
C |
(*uc->setRequestProperty)( uc, "Referer", "http://www.ist.co.uk/cgi-bin/example-form"); (*uc->setRequestProperty)( uc, "User-Agent", ""Mozilla/4.04 [en] (X11; I; SunOS 5.5 sun4m)"); (*uc->setRequestProperty)( uc, "Accept", "*/*"); (*uc->setRequestProperty)( uc, "Accept-Language", "en"); (*uc->setRequestProperty)( uc, "Accept-Charset", "iso-8859-1,*,utf-8"); (*uc->setRequestProperty)( uc, "Content-type", "application/x-www-form-urlencoded"); |
|
C++ |
uc->setRequestProperty( "Referer", "http://www.ist.co.uk/cgi-bin/example-form"); uc->setRequestProperty( "User-Agent", ""Mozilla/4.04 [en] (X11; I; SunOS 5.5 sun4m)"); uc->setRequestProperty( "Accept", "*/*"); uc->setRequestProperty( "Accept-Language", "en"); uc->setRequestProperty( "Accept-Charset", "iso-8859-1,*,utf-8"); uc->setRequestProperty( "Content-type", "application/x-www-form-urlencoded"); |
|
Java |
uc.setRequestProperty( "Referer", "http://www.ist.co.uk/cgi-bin/example-form"); uc.setRequestProperty( "User-Agent", ""Mozilla/4.04 [en] (X11; I; SunOS 5.5 sun4m)"); uc.setRequestProperty( "Accept", "*/*"); uc.setRequestProperty( "Accept-Language", "en"); uc.setRequestProperty( "Accept-Charset", "iso-8859-1,*,utf-8"); uc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded"); |
接続の OutputStream にデータを送信する | ||
---|---|---|
言語 | 使用法 | |
C |
OutputStream * o = (*uc->getOutputStream)( uc); PrintWriter * pw = newPrintWriter( o); (*pw->println)( pw, "firstname=Fred&secondname=Bloggs"); (*pw->delete)( pw); |
|
C++ |
OutputStream * o = uc->getOutputStream(); PrintWriter * pw = new PrintWriter( o); pw->println("firstname=Fred&secondname=Bloggs"); delete pw; |
|
Java |
OutputStream o = uc.getOutputStream(); PrintWriter pw = new PrintWriter( o); pw.println("firstname=Fred&secondname=Bloggs"); |
Web サーバーにグループデータを POST する | ||
---|---|---|
言語 | 使用法 | |
C |
int exampleSendHandler( uc, csdata) URLConnection * uc; sc_stdcs_t * csdata; { OutputStream * o; PrintWriter * pw; MyGroup_t * mygroup = (MyGroup_t*) csdata->group; char buf[128]; (*uc->setRequestProperty)( uc, "Content-type", "application/x-www-form-urlencoded"); o = (*uc->getOutputStream)( uc); pw = newPrintWriter( o); (void) sprintf( buf, "selection=%s&toggle=%s", SC_GET(SelectionByName,mygroup->optionMenu1, SC_GET(State,mygroup->toggle1)); (*pw->println)( pw, buf); (*pw->delete)( pw); return 1; } |
|
C++ |
int exampleSendHandler( URLConnection * uc, sc_stdcs_c* csdata) { OutputStream * o; PrintWriter * pw; MyGroup_c * mygroup = (MyGroup_c*) csdata->getGroup(); char buf[128]; uc->setRequestProperty("Content-type", "application/x-www-form-urlencoded"); o = uc->getOutputStream(); pw = new PrintWriter( o); (void) sprintf( buf, "selection=%s&toggle=%s", mygroup->optionMenu1->getSelectionByName(), mygroup->toggle1->getState()); pw->println(buf); delete pw; return 1; } |
|
Java |
public class exampleSendHandler_c extends SCOutputDataHandler { public void doit( URLConnection uc, SCStdCS csdata) OutputStream o; PrintWriter pw; // 注: JDK 1.0 では PrintStream を使用する MyGroup_c mygroup = (MyGroup_c) csdata.getGroup(); uc.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); o = uc.getOutputStream(); pw = new PrintWriter( o); String buf = "selection=" + mygroup.optionMenu1.getSelectionByName(), "&toggle=" + mygroup.toggle1.getState(); pw.println(buf); } } |