我知道,可是相应的url不能直接写,好像要自己写http头,但是我写了出现如下的错误
HTTP/1.1 405 Method not allowed
Date: Thu, 25 Sep 2003 12:03:07 GMT
Server: Microsoft-IIS/5.0
Vary: *
Allow: OPTIONS, TRACE, GET, HEAD不知道是不是我写的方法不对,请指教
HTTP/1.1 405 Method not allowed
Date: Thu, 25 Sep 2003 12:03:07 GMT
Server: Microsoft-IIS/5.0
Vary: *
Allow: OPTIONS, TRACE, GET, HEAD不知道是不是我写的方法不对,请指教
<input type=hidden name=user>
<input type=hidden name=pass>
</form>
<script language=JavaScript>
myform.user='houzongming';
myform.pass='mima';
myform.submit;
</script>
发送必须是这种格式的,特别是回车换行,多个少个都不行:
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;
}}具体的协议怎样的w3c有关于Http的解释,忘记在哪儿了,自己找吧
如果对传文件的不是这样的得查文档
import java.net.*;
import java.util.*;public class HttpCookie {
private static String[] header = {"POST /query2.asp HTTP/1.1\r\n",
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*\r\n",
"Referer: http://www.e-shrailway.com/\r\n",
"Accept-Language: zh-cn\r\n",
"Content-Type: application/x-www-form-urlencoded\r\n",
"Accept-Encoding: gzip, deflate\r\n",
"User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n",
"Host: www.e-shrailway.com\r\n",
"Content-Length: 25\r\n",
"Connection: Keep-Alive",
"Cache-Control: no-cache",
"Cookie: ASPSESSIONIDAATDDDCT=GACHBMLCOJIFECDHEGCHC\r\n\r\n",
"T_checi=k64&message2=true"}; public static void main(String[] args){
try{
//下面获得从ie登陆首页的cookie
URL url2 = new URL("http://www.e-shrailway.com");
URLConnection uc2 = url2.openConnection();
String cookie = uc2.getHeaderField("Set-Cookie");
cookie = "Cookie:" + cookie.substring(0,cookie.length()-8) + "\r\n\r\n";
header[11] = cookie;
//要发送的http头
String cmd = "";
for(int i = 0; i < header.length; i++){
cmd += header[i];
} URL url = new URL("http://www.e-shrailway.com");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
BufferedOutputStream sender = new BufferedOutputStream(uc.getOutputStream());
sender.write(cmd.getBytes(),0,cmd.length());
sender.flush(); String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}catch(java.net.MalformedURLException e){
e.printStackTrace();
}catch(java.io.IOException e){
e.printStackTrace();
}
}
}
怎么没人回答啊,立即就给分的,不够再加