如何在java的socket程序中使用http的if-modified-since之类的条件?拜托各位救我100分

match0116 2002-05-08 06:57:16
小弟对java一窍不通却被导师要求做这个!到处都找不到例子。
要在这个程序中加入if-modified-since,等有条件的http的get命令?
(此程序是从server端得到一个html的文件)。我想用if-modified-since来判断
server的那个html文件是否被改动过。
谢谢了。下星期再不会我就死定了!
另外这个程序关不掉是怎么回事,有人能帮我改改么?

import java.net.*;
import java.io.*;

public class SimpleWebClient
{
public static void main (String[] args)
{
Socket sock;
InputStream isIn;
PrintStream psOut;
byte abDataIn[] = new byte[1024];
int iNumBytes;

try
{
sock = new Socket ("192.168.0.1", 80);
isIn = sock.getInputStream ();
psOut = new PrintStream (sock.getOutputStream ());

System.out.println ("Clnt: Sending a GET request...");
psOut.println (
"GET /index.html HTTP/1.1 ");
psOut.println ("Connection: Keep-Alive");
psOut.println ("User-Agent: Mozilla/2.0GoldB1 (Win95; I)");
psOut.println ("Pragma: no-cache");
psOut.println ("Host: localhost");
psOut.println ("Accept: image/gif, image/x-xbitmap, "
+ "image/jpeg, image/pjpeg, */*");
psOut.println ("");
System.out.println ("Clnt: Sent a GET request...\n");

while (true)
{
iNumBytes = isIn.read (abDataIn, 0, 1024);
if (iNumBytes < 0)
{
break;
}

System.out.println ("-------------Received--------------- "
+ iNumBytes + " bytes");
System.out.println (new String (abDataIn, 0, 0, iNumBytes));
System.out.println ("------------------------------------\n");
}
}

catch (Exception exception)
{
System.out.println ("Clnt: Exception interacting with socket: "
+ exception);
}
}
}



...全文
334 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
match0116 2002-05-09
  • 打赏
  • 举报
回复
to yang_sun: 谢谢了,我给你发了邮件,收到了么?
Yang_Sun 2002-05-08
  • 打赏
  • 举报
回复
我给你把程序死的问题解决了,另外的逻辑我把思路写在程序中了

import java.net.*;
import java.io.*;

public class SimpleWebClient {
public static void main(String[] args){
Socket sock = null;
InputStream isIn = null;
PrintStream psOut = null;
byte abDataIn[] = new byte[1024];
int iNumBytes;

try {
//I make the web server to my localhost,and port 8000
sock = new Socket("127.0.0.1", 8000);
isIn = sock.getInputStream();
psOut = new PrintStream(sock.getOutputStream());

System.out.println("Clnt: Sending a GET request...");
psOut.println("GET /index.html HTTP/1.1");
psOut.println("Connection: Keep-Alive");
psOut.println("User-Agent: Mozilla/2.0GoldB1 (Win95; I)");
psOut.println("Pragma: no-cache");
psOut.println("Host: localhost");
psOut.println("Accept: image/gif, image/x-xbitmap, "
+ "image/jpeg, image/pjpeg, */*");
psOut.println ("");
System.out.println("Clnt: Sent a GET request...\n");

/*
while(true){
//the program will wait the inputstream to receive the other 1024 bytes
//if can not get the bytes, the program will freeze util get the bytes
iNumBytes = isIn.read(abDataIn, 0, 1024);
if (iNumBytes < 0) {

break;
}
System.out.println("-------------Received--------------- "
+ iNumBytes + " bytes");
System.out.println(new String (abDataIn, 0, 0, iNumBytes));
System.out.println("------------------------------------\n");
}*/
while(isIn.available() > 0){
iNumBytes = isIn.read(abDataIn, 0, 1024);
/*
come here you can get the last-modified information
use this infomation to compare to your given time point,
if the last-modified field is later than the time your teacher given
then return the full response
else return code 304
It is too cumbersome to write that code. And because your use socket and inputstream
instead of Http... class, so I can not use the method Http... Class defined
I think u can use readLine() method, so u can deal with each Line one by one
if u find the line with "last-modified", then get the time info
just so so, good luck to u
if u can not understand what i said, email to sunyang97@mails.tsinghua.edu.cn
Good Night

ps: the program not can exit with no error
*/

System.out.println("-------------Received--------------- "
+ iNumBytes + " bytes");
System.out.println(new String (abDataIn, 0, 0, iNumBytes));
System.out.println("------------------------------------\n");
}
} catch (Exception exception) {
System.out.println ("Clnt: Exception interacting with socket: " + exception);
} finally {
try {
isIn.close();
psOut.close();
sock.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}

match0116 2002-05-08
  • 打赏
  • 举报
回复
路人甲的东西我看到了,但是不明白是什么意思,我是一点都没有java基础的,只会考书上的例子,然后执行之。

其实导师的要求是这样的:1: 用java写一个简单的socket程序,可以从server(windows 2000 IIS做服务器)取一个html的文件回来,把它显示在dos下。2:同时还要显示出http的respone header的信息(一定要包括Expire time和Etag的信息)。(这两个要求我贴出的程序都可以实现了。(不过我的程序是从书上找的,有问题,执行完后不能关闭。why?))**2: 在程序里还要求可以使用http中的If-Modified-Since这个条件,并证明这个条件起了作用。例如:如果在某个特定时间前server端的html被改变过执行完这个程序后,就正常返回那个html的文件,要是在这个时间前html文件没有被改动过server就只返回一个304的代码,表示文件没有被改动过。
当然还有If-Unmodified-Since,If-Match 和If-None-Match(用于比较Etag里的信息),这些条件都是http中的东西,被要求要和程序中的get request配合使用。我是实在不知道怎么在java socket的程序中使用这些http的条件。
好了,整个我的project就是这样了,不知道是不是够详细。 要是谁有办法一定要帮帮我,高手们一定应该不会觉得这东西难吧,可是我在澳洲这鬼地方找资料都没有办法,英文又差,要是下个星期再交不了程序就死定了。(这个程序可就是1门课呀,2000澳币呀!)
我的email是match0116@sina.com 或者qm56@uow.edu.au(英文)
Yang_Sun 2002-05-08
  • 打赏
  • 举报
回复
好像路人甲不是给你解决了吗?

你的应用逻辑到底是干什么的,说具体一些,我们也好帮忙,你就说一个if modify since,我还是不明白什么意思呀。到底是你们导师给你一个点时间,比如今天18点,然后如果晚于这个时间点的修改,都重新下载的意思吗?

你写的长点,自然有人帮忙
match0116 2002-05-08
  • 打赏
  • 举报
回复
就没有人帮帮忙么?
【3D应力敏感度析拓扑优化】【基于p-范数全局应力衡量的3D敏感度析】基于伴随方法的有限元析和p-范数应力敏感度析(Matlab代码实现)内容概要:本文档介绍了基于伴随方法的有限元析与p-范数全局应力衡量的3D应力敏感度析,并结合拓扑优化技术,提供了完整的Matlab代码实现方案。该方法通过有限元建模计算结构在载荷作用下的应力布,采用p-范数对全局应力进行有效聚合,避免传统方法应力约束过多的问题,进而利用伴随法高效求解设计变量对应力的敏感度,为结构优化提供关键梯度信息。整个流程涵盖了从有限元析、应力评估到敏感度计算的核心环节,适用于复杂三维结构的轻量化与高强度设计。; 适合人群:具备有限元析基础、拓扑优化背景及Matlab编程能力的研究生、科研人员与工程技术人员,尤其适合从事结构设计、力学仿真与多学科优化的相关从业者; 使用场景及目标:①用于实现高精度三维结构的应力约束拓扑优化;②帮助理解伴随法在敏感度的应用原理与编程实现;③服务于科研复现、论文写作与工程项目的结构性能提升需求; 阅读建议:建议读者结合有限元理论与优化算法知识,逐步调试Matlab代码,重点关注伴随方程的构建与p-范数的数值处理技巧,以深入掌握方法本质并实现个性化拓展。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧