post请求页面的问题

clius 2005-01-13 01:51:43
一个普通的HTML页面中有一个form,定义如下:
<form name="form2" method="post" action="query.asp" >
form里有一个文本输入框和一个submit按钮,输入值并点击submit按钮后提交给/query.asp文件.怎么用java模拟这个submit按钮,带上一个参数,提交并访问/query.asp页面的内容?(注意:/query.asp的内容是由post那个参数的不同而产生不同的内容!)

...全文
421 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
clius 2005-01-14
  • 打赏
  • 举报
回复
以下是抛出的异常

java.io.IOException: Server returned HTTP response code: 405 for URL: http://www.e-shrailway.com


怎么没人回答啊,立即就给分的,不够再加
clius 2005-01-14
  • 打赏
  • 举报
回复
下面是我的程序,请高手看看哪里错了啊

import java.io.*;
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();
}
}
}
eureka0891 2005-01-13
  • 打赏
  • 举报
回复
用这种格式:
发送必须是这种格式的,特别是回车换行,多个少个都不行:
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: 30

alumniuname=kkk&alumniupwd=kkk



source:
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的解释,忘记在哪儿了,自己找吧
如果对传文件的不是这样的得查文档
clius 2005-01-13
  • 打赏
  • 举报
回复
谢谢slaser,可是我没什么头绪,能否说的详细些
slaser 2005-01-13
  • 打赏
  • 举报
回复
客户端用java有什么好的,看看apache上的HttpClient吧。免得写那么多东西。
clius 2005-01-13
  • 打赏
  • 举报
回复
我首先建立了一个连接取得cookie,然后在建立一个连接使用获得的cookie连同文本框的内容一起发到query.asp中,为什么总是出现 Server returned HTTP response code: 405 的错误呢?
clius 2005-01-13
  • 打赏
  • 举报
回复
请高手指定啊,急死了
clius 2005-01-13
  • 打赏
  • 举报
回复
to fishbob21:我用了get,还是不行

to hzmhzmhzm:我这里用java写的,是程序的一小部分,主要去网上取点数据,呵呵,javascript不太会。

我大概知道就是cookie的问题,但是不知道具体怎么操作。

eric_uugames 2005-01-13
  • 打赏
  • 举报
回复
使用javascript不久得了吗

<form name=myform action=..>
<input type=hidden name=user>
<input type=hidden name=pass>
</form>
<script language=JavaScript>
myform.user='houzongming';
myform.pass='mima';
myform.submit;
</script>
fishbob21 2005-01-13
  • 打赏
  • 举报
回复
把method改成GET看看
clius 2005-01-13
  • 打赏
  • 举报
回复
我知道,可是相应的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

不知道是不是我写的方法不对,请指教
javafaq2004 2005-01-13
  • 打赏
  • 举报
回复
Socket或者Url

62,615

社区成员

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

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