使用Java套接字获取GET请求

编程入门 行业动态 更新时间:2024-10-25 17:22:27
本文介绍了使用Java套接字获取GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个简单的程序来向特定网址发送get请求 badunetworks/about / 。如果我将其发送到 badunetworks ,请求仍然有效,但我需要将其发送到关于页面。

I am writing a simple program to send a get request to a specific url "badunetworks/about/". The request works if I send it to "badunetworks" but I need to send it to the about page.

package badunetworks; import java.io.*; import java.*; public class GetRequest { public static void main(String[] args) throws Exception { GetRequest getReq = new GetRequest(); //Runs SendReq passing in the url and port from the command line getReq.SendReq("www.badunetworks/about/", 80); } public void SendReq(String url, int port) throws Exception { //Instantiate a new socket Socket s = new Socket("www.badunetworks/about/", port); //Instantiates a new PrintWriter passing in the sockets output stream PrintWriter wtr = new PrintWriter(s.getOutputStream()); //Prints the request string to the output stream wtr.println("GET / HTTP/1.1"); wtr.println("Host: www.badunetworks"); wtr.println(""); wtr.flush(); //Creates a BufferedReader that contains the server response BufferedReader bufRead = new BufferedReader(new InputStreamReader(s.getInputStream())); String outStr; //Prints each line of the response while((outStr = bufRead.readLine()) != null){ System.out.println(outStr); } //Closes out buffer and writer bufRead.close(); wtr.close(); } }

推荐答案

如果about页面链接是about.html,那么你已将此行wtr.println(GET / HTTP / 1.1)更改为wtr.println(GET /about.html HTTP / 1.1) )。

if the about page link is about.html ,then you have change this line wtr.println("GET / HTTP/1.1") into wtr.println("GET /about.html HTTP/1.1").

在套接字创建中删除/ about

in socket creation remove the /about

wtr.println(GET / HTTP / 1.1 ); --->此行调用您指定的主机的主页。

wtr.println("GET / HTTP/1.1");--->this line call the home page of the host you specified.

更多推荐

使用Java套接字获取GET请求

本文发布于:2023-08-07 03:46:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!