Java Perl中有没有类似于HttpURLConnection的东西?
我想创建一个到PHP脚本的HttpUrl连接,并获取脚本返回的HTTP响应。有没有办法在Perl中实现这一点 简而言之,我希望Perl等效于以下内容:Java Perl中有没有类似于HttpURLConnection的东西?,java,perl,cgi,cookies,httpurlconnection,Java,Perl,Cgi,Cookies,Httpurlconnection,我想创建一个到PHP脚本的HttpUrl连接,并获取脚本返回的HTTP响应。有没有办法在Perl中实现这一点 简而言之,我希望Perl等效于以下内容: java.net.URL url = new java.net.URL(urlPath); java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); conn.s
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模块,但找不到任何模块。
任何帮助都会非常有用。试试:
取决于你的目标,可能更合适。在你的例子中,为什么要这样做:$ua->cookie\u jar({file=>“$ENV{HOME}/.cookies.txt});你能详细解释一下吗?@Sam,我想你想用
$ua->cookie\u jar({})创建一个内存中的cookie jar
然后调用$ua->cookie\u jar->set\u cookie()上面的示例中,“cookie”是一个字符串
# 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";
}