perljunkie has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to interact with webserver. It’s a simple Get and Post kind of request response methods implemented at the server. All I wanted to do is use HTTP1.1 Post x2 times as shown below:
POST http://someip:port/ HTTP/1.1 User-Agent: Perlprogram/0.1 Host: someip:port Accept: application/text Content-Type: application/text Content-Length: xxx UserID: Username, Password: password HTTP/1.1 200 OK Date: Mon, 15 Dec 2008 20:49:26 CST Cache-Control: no-store Content-Type: application/text Content-Length: xxx Login Successful POST http://someip:port/ HTTP/1.1 User-Agent: Perlprogram/0.1 Host: someip:port Accept: application/text Content-Type: application/text Content-Length: xxx Send some conf file HTTP/1.1 200 OK Date: Mon, 15 Dec 2008 20:49:27 CST Cache-Control: no-store Content-Type: application/text Content-Length: xxx Here it is........
I tried using Socket/HTTP:NET/LWP etc with no success. Below is my example code.
#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common; my $message; my $userAgent = LWP::UserAgent->new(agent => 'Perlprogram/0.1'); $message = "UserID: Username, Password: password"; my $response = $userAgent->request(POST 'http://someip:port/', Content_Type => 'application/text', Connection => 'Keep-alive', Content => $message); if ($response->is_success) { print $response->content; } else { print $response->status_line, "\n"; }
I tried to insert another post code in or run them in different get post .pl files yield no help. Because server sees connection closed due to post close response.
POST / HTTP/1.1 TE: deflate,gzip;q=0.3 Connection: Keep-alive, TE, close Host: someip:port User-Agent: Perlprogram/0.1 Content-Length: xxx Content-Type: application/text
I see something like above in the wireshark which means my connection is TE,Close by some reason. So after webserver's response my connection and login becomes invalid. Due to this I am unable to retrive file with second post. Second observation I made was in the TCP header my sequence number starts with 1 instead of continuation. E.g.: my client program should say Next Sequence number will be: 343 and it should go to that sequence number and send post message so that webserver can reply. Any help will be appreciated.

Replies are listed 'Best First'.
Re: HTTP Post read response , Post again
by ikegami (Patriarch) on Dec 18, 2008 at 20:49 UTC

    All I wanted to do is use HTTP1.1 Post x2 times as shown below

    I believe you mean to do two requests on the same connection? Yet you do nothing to tell LWP to do so (assuming it's even capable of doing so).

    So after webserver's response my connection and login becomes invalid.

    Your login only lasts as long as the connection??? What kind of server is that! That defies the normal functioning of HTTP.

      If it is indeed a login problem. The issue is that the OP is probably not keeping up with cookies.

      Since HTTP is a stateless protocol, the server has no concept of a login. The application may have a session concept which is normally tracked with cookies. This is why I suspect the problem is cookie related. As I recall, LWP::UserAgent has a feature for using HTTP::Cookies to track a cookie jar that handles this issue.

      G. Wade
        Looks like it. Any sample code to begin with? Will I be able to handle Java Session using IOR?
      I believe you mean to do two requests on the same connection? Yet you do nothing to tell LWP to do so (assuming it's even capable of doing so). Yes. HTTP 1.1 Post; read response. If successful; post again with new request. But on the same connection. If you know TCP you will see in TCP header Source Port, Destination Port, Sequence Number, next sequence number, ack number. For two consutive posts TCP sequence number always starts from 1. Webserver is: thttpd.
      #!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common; #Post 1 for login my $userAgent = LWP::UserAgent->new(agent => 'Perlprogram/0.1'); my $message = "UserID: Username, Password: passwordr"; my $response = $userAgent->request(POST 'http://someip:port/', Content_Type => 'application/text', Connection => 'Keep-alive', Content => $message); #print $response->error_as_HTML unless $response->is_success; #print $response->as_string; if ($response->is_success) { print $response->content; #post again if success my $userAgent2 = LWP::UserAgent->new(agent => 'Perlprogram/0.1'); my $message2 = "GetParam"; my $response2 = $userAgent2->request(POST 'http://someip:port/', Content_Type => 'application/text', Connection => 'Keep-alive', Content => $message2); print $response2->error_as_HTML unless $response2->is_success; print $response2->as_string; } else { print $response->status_line, "\n"; }
      I see in some new response my Keep-alive syntext was not proper. Let me modify and will post you result. Thanks for replies guys.....
Re: HTTP Post read response , Post again
by zwon (Abbot) on Dec 18, 2008 at 23:15 UTC
    Try to specify keep_alive option when creating user agent instead of Connection parameter in request. Also may be you really should use cookies as noted above.
    my $userAgent = LWP::UserAgent->new( agent => 'Perlprogram/0.1', keep_alive => 1, ); $userAgent->cookie_jar(HTTP::Cookies->new());
      thanks for your reply. I modified my code to have keep-alive. Below is output. It still send Connection as TE, Close. However I see Keepalive message send but ignored.
      POST / HTTP/1.1 TE: deflate,gzip;q=0.3 Connection: TE, close Host: someip:port User-Agent: Perlprogram/0.1 Content-Length: 218 Content-Type: application/text Keep-Alive: 1
        Probably something wrong in your code, I've tested following script:
        use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common; my $message; my $userAgent = LWP::UserAgent->new( agent => 'Perlprogram/0.1', keep_alive => 1 ); $message = "UserID: Username, Password: password"; my $response = $userAgent->request( POST 'http://example.com/', Content_Type => 'application/text', Content => $message );
        And here is the request I've got:
        POST / HTTP/1.1 TE: deflate,gzip;q=0.3 Keep-Alive: 300 Connection: Keep-Alive, TE Host: example.com User-Agent: Perlprogram/0.1 Content-Length: 36 Content-Type: application/text UserID: Username, Password: password
Re: HTTP Post read response , Post again
by poolpi (Hermit) on Dec 19, 2008 at 14:41 UTC

    Due to this I am unable to retrive file with second post.

    I think you're trying to access a protected file,
    you may try something like this:

    ( See lwpcook )

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => 'http://someip:port/'); $req->authorization_basic('Username', 'password'); print $ua->request($req)->as_string;


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      No login is no issue, because when i enter no password I receive negative response. When both userid and password correct I see login successful message sent by server.
        You can disable "TE" header sending by tweaking LWP::Protocol::http properties. Add the following to your code and TE will not sent.
        push(@LWP::Protocol::http::EXTRA_SOCK_OPTS, SendTE => 0);
        Regards,