当前位置: 技术问答>java相关
有关java与web服务器的通讯的问题
来源: 互联网 发布时间:2014-12-26
本文导语: 各位大侠,小弟正在想编一个java与web服务器通讯的程序,我想用“报路获取文件“的方法,即向web服务器发送get请求的方式,但是我却调不通,现在不知道怎么办,望各位大虾指教,小弟不会吝啬分数的!!! ...
各位大侠,小弟正在想编一个java与web服务器通讯的程序,我想用“报路获取文件“的方法,即向web服务器发送get请求的方式,但是我却调不通,现在不知道怎么办,望各位大虾指教,小弟不会吝啬分数的!!!
|
我用SOCKET编了一个TEST程序,我运行过了,没有问题。
仅共参考:
Sample:
import java.io.*;
import java.net.*;
public class Test {
private Socket sock = null;
private BufferedReader inReader = null;
private BufferedWriter outWriter = null;
public Test(String host)
throws UnknownHostException,IOException {
this(host,80);
}
public Test(String host,int port)
throws UnknownHostException,IOException {
sock = new Socket(host,port);
inReader = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
outWriter = new BufferedWriter(
new OutputStreamWriter(sock.getOutputStream()));
}
public void bye() throws IOException {
inReader.close();
inReader = null;
outWriter.close();
outWriter = null;
sock.close();
sock = null;
}
public static void main(String[] args) {
try {
Test t = new Test("www.263.net");
t.outWriter.write("GET /user/register.html HTTP/1.0rnrn");
t.outWriter.flush();
String text = null;
while((text = t.inReader.readLine()) != null) {
System.out.println(text);
}
t.bye();
}
catch(UnknownHostException unKnownHostError) {
unKnownHostError.printStackTrace();
System.exit(0);
}
catch(IOException ioError) {
ioError.printStackTrace();
System.exit(0);
}
}
}
仅共参考:
Sample:
import java.io.*;
import java.net.*;
public class Test {
private Socket sock = null;
private BufferedReader inReader = null;
private BufferedWriter outWriter = null;
public Test(String host)
throws UnknownHostException,IOException {
this(host,80);
}
public Test(String host,int port)
throws UnknownHostException,IOException {
sock = new Socket(host,port);
inReader = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
outWriter = new BufferedWriter(
new OutputStreamWriter(sock.getOutputStream()));
}
public void bye() throws IOException {
inReader.close();
inReader = null;
outWriter.close();
outWriter = null;
sock.close();
sock = null;
}
public static void main(String[] args) {
try {
Test t = new Test("www.263.net");
t.outWriter.write("GET /user/register.html HTTP/1.0rnrn");
t.outWriter.flush();
String text = null;
while((text = t.inReader.readLine()) != null) {
System.out.println(text);
}
t.bye();
}
catch(UnknownHostException unKnownHostError) {
unKnownHostError.printStackTrace();
System.exit(0);
}
catch(IOException ioError) {
ioError.printStackTrace();
System.exit(0);
}
}
}