fsockopen equivalent in JSP

Deadly 提交于 2019-12-01 11:13:46

问题


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);
}

回答1:


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/" />



回答2:


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){}
%>



回答3:


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();
}
%>


来源:https://stackoverflow.com/questions/4774055/fsockopen-equivalent-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!