JSP 中的 fsockopen 等效项

发布于 2024-10-13 12:25:43 字数 411 浏览 3 评论 0原文

我该如何将此代码转换为 JSP

任何帮助表示赞赏..!

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

How do i go about converting this code to JSP

Any help appreciated..!

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-10-20 12:25:43

为此,您可以使用 JSTL 标记库。首先在 jsp 顶部使用声明命名空间

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

,然后使用 c:import 标签包含指定 url 中的内容:

<c:import url="htp://www.example.com/" />

You can use the JSTL taglib for this. First declare the namespace at the top of the jsp using

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

And then use the c:import tag to include content from the specified url:

<c:import url="htp://www.example.com/" />
酒儿 2024-10-20 12:25:43

检查一下它是否有效......

<%@ page contentType="text/html" import="java.io.*, java.net.*" %>

<% 
    try {
        Socket s = new Socket("www.java2s.com", 80);

        BufferedReader in = new BufferedReader(new 
            InputStreamReader(s.getInputStream()));
        PrintWriter socketOut = new PrintWriter(s.getOutputStream());

        socketOut.print("GET /index.html\n\n");
        socketOut.flush();

        String line;

        while ((line = in.readLine()) != null){
            out.println(line);
        }

    } catch (Exception e){}
%>

Check this out it works.......

<%@ page contentType="text/html" import="java.io.*, java.net.*" %>

<% 
    try {
        Socket s = new Socket("www.java2s.com", 80);

        BufferedReader in = new BufferedReader(new 
            InputStreamReader(s.getInputStream()));
        PrintWriter socketOut = new PrintWriter(s.getOutputStream());

        socketOut.print("GET /index.html\n\n");
        socketOut.flush();

        String line;

        while ((line = in.readLine()) != null){
            out.println(line);
        }

    } catch (Exception e){}
%>
記憶穿過時間隧道 2024-10-20 12:25:43

您可以使用 Apache HTTP 组件 库来完成此操作。类似下面的内容应该适用于 HTTP 组件库:

<%@ page import="org.apache.http.*, org.apache.http.impl.*, org.apache.http.params.*, org.apache.http.protocol.*, org.apache.http.message.BasicHttpRequest, org.apache.http.util.EntityUtils,  java.net.Socket" %>

<%
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);

HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
        new RequestContent(),
        new RequestTargetHost(),
        new RequestConnControl(),
        new RequestUserAgent(),
        new RequestExpectContinue()});

HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("www.example.com", 80);

DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

try 
{        
     Socket socket = new Socket(host.getHostName(), host.getPort());
     conn.bind(socket, params);

     BasicHttpRequest request = new BasicHttpRequest("GET", "/");


     request.setParams(params);
     httpexecutor.preProcess(request, httpproc, context);
     HttpResponse response = httpexecutor.execute(request, conn, context);
     response.setParams(params);
     httpexecutor.postProcess(response, httpproc, context);

%>
<%=EntityUtils.toString(response.getEntity())%>
<%
} 
finally 
{
    conn.close();
}
%>

You can do this with the Apache HTTP Components library. Something like the following should work with the HTTP Components library:

<%@ page import="org.apache.http.*, org.apache.http.impl.*, org.apache.http.params.*, org.apache.http.protocol.*, org.apache.http.message.BasicHttpRequest, org.apache.http.util.EntityUtils,  java.net.Socket" %>

<%
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);

HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
        new RequestContent(),
        new RequestTargetHost(),
        new RequestConnControl(),
        new RequestUserAgent(),
        new RequestExpectContinue()});

HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("www.example.com", 80);

DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

try 
{        
     Socket socket = new Socket(host.getHostName(), host.getPort());
     conn.bind(socket, params);

     BasicHttpRequest request = new BasicHttpRequest("GET", "/");


     request.setParams(params);
     httpexecutor.preProcess(request, httpproc, context);
     HttpResponse response = httpexecutor.execute(request, conn, context);
     response.setParams(params);
     httpexecutor.postProcess(response, httpproc, context);

%>
<%=EntityUtils.toString(response.getEntity())%>
<%
} 
finally 
{
    conn.close();
}
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文