パスワードを忘れた? アカウント作成
452766 journal

fslashtの日記: はてなダイアリー自動転記スクリプト開発(6) スクリプトによるPOST実験 6

日記 by fslasht

STATUS: 故障中

 3回目4回目で調べた「はてな」へのリクエスト内容を元にプログラムを組んでみました。
 どうやらちゃんと機能しているようです。

・PHP CLI版用スクリプト:posthatena.php
・テスト用データ:post.txt

※文字コードはeuc-jp、改行コードは cr+lf (0x0d,0x0a)
※プログラムは、Linux(kernel 2.4.19) + PHP4.1.2 CLI版で検証しています
※テストデータは、cookieに認証用のキーが含まれているため、公開にあたりその部分を改変してあります。(Cookieヘッダの rk=以降)

 このプログラムでは、同一ディレクトリにあるテスト用データファイル「post.txt」を「はてな」のWebサーバ(d.hatena.ne.jp:80)に送信し、HTTPの応答コードを取得します。
 やってることもコード内容もえらい単純なものです。
 送信内容は、実際に「はてな」に投稿を行った際のパケットをキャプチャしたデータほぼそのままです。
 このままだと余計なヘッダが多いので最終的には削ることになります(User-Agentも「Mozilla/4.0」のまんまですし)。

● 「はてな」からの応答内容

書き込み成功時
  (HTTP/1.1 302 Moved)

 書き込み成功時は、書き込んだ日付の日記にリダイレクトされるようです。

【ヘッダ内容】
location: ./20031214

認証失敗時
  (HTTP/1.1 302 Moved)

 Cookieのセッションキー不正によりユーザー認証に失敗した場合は、トップページにリダイレクトされるようです。

【ヘッダ内容】
location: /

サーバーエラー (POST内容不正)
(HTTP/1.1 500 Internal Server Error)

 POST内容が不正の場合は、この様な内容でエラーが返ってきました。
 post.txtを改変している際に次の要因で発生しました。

・Content-Lengthを省略した場合(手抜きを仕様とした)
・boundaryの指定が食い違っている場合。
 データ部で実際に使用されるboundaryは、「Content-Type:」ヘッダでのboundary文字列の前に「--」をつけなければならない。

サーバーエラー(Content-Length不正時等)
(HTTP/1.1 500 Internal Server Error)

 Content-Lengthで指定するデータ長を間違えた場合、このエラーになりました。

● 他のユーザーに適用する場合の変更点

 post.txtの以下の箇所を変更します。

・ユーザー名変更
 1行目と12行目のユーザー名「fslasht」を変更
・セッションキー変更
 12行目の「rk=」以降のセッションキーを変更
 ※セッションキーはブラウザのCookieに記録されています。参照

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
  • #!/usr/local/bin/php
    Post Hatena diary test
    2003.12.14 FUKAYA Takashi (F/T)

    <?php

        // テキストファイルを読み込む
        function ReadStr( $strFile = "" ) {
            $str = "";
            $fd = fopen( $strFile , "r" );
            if ( $fd ) {
                while (!feof($fd)) {
                    $str .= fread( $fd , 1024 );
                }
                fclose($fd);
            }else{
                return "";
            }
            return $str;
        }

        // Webサーバへリクエストを送る (偽春奈に発言させる)
        //        入力パラメータ
        //               $adr:サーバのアドレス(ドメイン名)
        //              $data:送信データ
        //              $recv:受信データ(のバッファ)
        //        戻り値:エラー/ステータスコード
        //                 -1:接続が確立できなかった
        //               -2:webサーバから正常な応答がなかった
        //            0以上:HTTP応答コード
        function SendRequest($adr,$data,&$recv) {
            $errno = 0;
            $hostadr = gethostbyname( $adr );
            $lastsockerrmsg = 0;
            $recv = "";

            $sid = fsockopen( $hostadr , 80 , $errno , $lastsockerrmsg , 10 );
            if ( $sid == 0 ) return -1;                                // 接続エラー

            fputs( $sid ,$data);

            while (!feof($sid)) {
                $recv =$recv . fgets ($sid,128);
            }
            fclose( $sid );

            if ( substr($recv,0,9) != "HTTP/1.1 " ) return -2;        // 応答文字列エラー
            return  (int)substr($recv,9,3);                            // 成功
        }

    // 送信ファイルを読み込む
    $str = ReadStr( "post.txt" );

    // 送信! (POST)
    $recv = "";
    $stat = SendRequest( "d.hatena.ne.jp",$str,$recv);

    // 受信した内容を表示
    print( "Status:$stat\n" );
    print( "***** BEGIN\n" );
    print( $recv );
    print( "***** END\n" );
    ?>

    program has done.
  • by fslasht (3370) on 2003年12月15日 3時04分 (#454840) ホームページ 日記
    POST /fslasht/edit HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-gsarcade-launch, application/x-shockwave-flash, */*
    Referer: http://d.hatena.ne.jp/fslasht/edit
    Accept-Language: ja
    Content-Type: multipart/form-data; boundary=-BOUNDARY---
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
    Host: d.hatena.ne.jp
    Content-Length: 723
    Connection: Keep-Alive
    Cache-Control: no-cache
    Cookie: name=fslasht; rk=kokonisessionidgahairunodesu

    ---BOUNDARY---
    Content-Disposition: form-data; name="mode"

    enter
    ---BOUNDARY---
    Content-Disposition: form-data; name="year"

    2003
    ---BOUNDARY---
    Content-Disposition: form-data; name="month"

    12
    ---BOUNDARY---
    Content-Disposition: form-data; name="day"

    14
    ---BOUNDARY---
    Content-Disposition: form-data; name="title"

    TestTitle
    ---BOUNDARY---
    Content-Disposition: form-data; name="body"

    テスト本文
    TEST POST
    ---BOUNDARY---
    Content-Disposition: form-data; name="image"; filename=""
    Content-Type: application/octet-stream

    ---BOUNDARY---
    Content-Disposition: form-data; name="imagetitle"

    ---BOUNDARY---
    Content-Disposition: form-data; name="edit"

    この内容を登録する
    ---BOUNDARY---
  • HTTP/1.1 302 Moved
    Date: Sun, 14 Dec 2003 17:21:20 GMT
    Server: Apache/1.3.27 (Unix)  (Vine/Linux) mod_perl/1.26
    location: ./20031214
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/plain

    0
  • HTTP/1.1 302 Moved
    Date: Sun, 14 Dec 2003 17:09:52 GMT
    Server: Apache/1.3.27 (Unix)  (Vine/Linux) mod_perl/1.26
    location: /
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/plain

    0
  • HTTP/1.1 500 Internal Server Error
    Date: Sun, 14 Dec 2003 17:03:01 GMT
    Server: Apache/1.3.27 (Unix)  (Vine/Linux) mod_perl/1.26
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=iso-8859-1

    255
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <HTML><HEAD>
    <TITLE>500 Internal Server Error</TITLE>
    </HEAD><BODY>
    <H1>Internal Server Error</H1>
    The server encountered an internal error or
    misconfiguration and was unable to complete
    your request.<P>
    Please contact the server administrator,
    root@localhost and inform them of the time the error occurred,
    and anything you might have done that may have
    caused the error.<P>
    More information about this error may be available
    in the server error log.<P>
    <HR>
    <ADDRESS>Apache/1.3.27 Server at diary.hatena.ne.jp Port 80</ADDRESS>
    </BODY></HTML>

    0
  • HTTP/1.0 500 Internal Server Error
    Content-Type: text/html
    Content-Length: 152

    <html><head><title>500 Internal Server Error</title></head><body><h1>500 Interna
    l Server Error</h1><p>Sorry... It's under maintenance.</p></body></html>
typodupeerror

Stay hungry, Stay foolish. -- Steven Paul Jobs

読み込み中...