socket("123.123.123.123","80")
output stream write("GET /project/login.jsp HTTP/1.0\n\n");
read input stream
output stream write("GET /project/login.jsp HTTP/1.0\n\n");
read input stream
解决方案 »
- @XmlType问题 急急急急急急......
- 为什么不能防止多次启动呢?
- 无法编译java程序
- 如何判断一个字符串里包含多少个S这样的字符
- Axis+tomcat的问题
- 请教一个JAVA程序的问题?
- 分数太多,大家共享
- SpringBoot 继承swagger 访问404的问题
- maven多模块中springmvc注入service出错时报错
- 一个生产者与消费者问题,不明白为什么总是报错,题目是:有个苹果箱子,最多放5个苹果,一个人无限往里面放,一个人无限往外拿,假设苹果无限,实现这两个线程
- jboss中如何配置连接两个数据库?
- spring中在ModelAndView对应的jsp页面中用OutputStream输出图像的问题
URLConnection
这个可以。
只是抛出异常没有找到响应的host
因为123.123.123.123是不能访问的~
只能访问123.123.123.123/project
所以我向客户端的输出添加了输出流"GET /project HTTP/1.1\r\n\r\n"
但是还是没有能找到·~我郁闷啊~~~
比如服务器端设置了某些根目录是不能被访问的参数等good luck
URLConnection
试试 URL url = new URL("http://123.123.123.123/project/login.jsp");
因为现在的服务器都会检查host值的,多用于虚拟主机
GET /project HTTP/1.1\nHost: 123.123.123.123\n\n
你post的格式对不对啊.
我是这样post的:
POST http://news.sina.com.cn/c/2004-12-21/10084584671s.shtml HTTP/1.0
User-Agent: myselfHttp/1.0
Accept: www/source; text/html; image/gif; */*
Host: news.sina.com.cn
Content-type: application/x-www-form-urlencoded
Content-length: 30alumniuname=kkk&alumniupwd=kkksource:
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;/*
* Created on 2004-12-21
*
* To change the template for this generated file go to Window - Preferences -
* Java - Code Generation - Code and Comments
*//**
* @author Artimus
*
* To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class ConnectUrl { private static final String OPEN_URL = "http://news.sina.com.cn/c/2004-12-21/10084584671s.shtml"; public static void main(String[] args) {
Socket client = null;
BufferedOutputStream sender = null; try {
URL webURL = new URL(OPEN_URL);
InetAddress address = InetAddress.getByName(webURL.getHost());
int portTemp = webURL.getPort();
if (portTemp == -1) {
portTemp = 80;
}
client = new Socket(address, portTemp);
String content = "alumniuname=kkk&";
content += "alumniupwd=kkk"; String cmd = "POST " + OPEN_URL + " HTTP/1.0\r\n" + getBaseHeads();
cmd += "Host: " + webURL.getHost() + "\r\n";
cmd += "Content-type: application/x-www-form-urlencoded\r\n";
cmd += "Content-length: " + content.length() + "\r\n\r\n";
cmd += content + "\r\n";
System.out.println(cmd);
sender = new BufferedOutputStream(client.getOutputStream());
sender.write(cmd.getBytes(), 0, cmd.length());
sender.flush(); BufferedReader out = new BufferedReader(new InputStreamReader(
client.getInputStream()));
int b;
String line = null;
int i = 0;
if (out != null) {
while ((line = out.readLine()) != null) {
System.out.println(line);
}
out.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (client != null) {
client.close();
client = null;
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
/** **************************** */
public static String getBaseHeads() {
String inf = "User-Agent: myselfHttp/1.0\r\n"
+ "Accept: www/source; text/html; image/gif; */*\r\n";
return inf;
}}如果这样还不行,就不知道了