Есть ли что-то похожее на HttpURLConnection в Perl? - PullRequest
3 голосов
/ 09 июля 2009

Я хочу создать HTTPURLConnection к сценарию PHP и получить ответ HTTP, возвращаемый сценарием. Есть ли способ сделать это в Perl?

Короче говоря, я хочу, чтобы Perl соответствовал следующему:

            java.net.URL url = new java.net.URL(urlPath);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();

            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", Integer.toString(body.length())); 

            conn.setRequestProperty("Cookie", "ONTCred=" + cookie);

            conn.connect();

            java.io.PrintWriter pw = new java.io.PrintWriter(conn.getOutputStream());
            pw.print(body); // "send" the body
            pw.flush();
            pw.close();

            if (conn.getResponseCode() != java.net.HttpURLConnection.HTTP_OK) {
                    throw new java.io.IOException("Error on POST to " + url + ": " + conn.getResponseMessage());
            }

            // for debugging, if you want to see the header info, uncomment this section
            // for (String key : conn.getHeaderFields().keySet()) {
            //      System.out.println("header: '" + key + "' = '" + conn.getHeaderField(key) + "'");
            // }

Я пытаюсь найти похожий модуль perl, но не смог найти ни одного. Любая помощь будет очень полезна.

Ответы [ 2 ]

6 голосов
/ 09 июля 2009

Попробуйте LWP :

 # Create a user agent object
 use LWP::UserAgent;
 my $ua = LWP::UserAgent->new;

 # Create a request
 my $req = HTTP::Request->new(POST => $url);
 $req->content_type('application/x-www-form-urlencoded');

 # cookies 
 $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });

 # Pass request to the user agent and get a response back
 my $res = $ua->request($req);

 # Check the outcome of the response
 if ($res->is_success) {
    print $res->content;
 } else {
    print $res->status_line, "\n";
 }
0 голосов
/ 11 июля 2009

В зависимости от ваших целей, WWW :: Mechanize может подойти лучше.

...