本人是个java新手,老板为锻炼新人,所以出了些题目,要求是在一个象百度那样的网站(只有一个文本框,一个按钮的页面),每输入一次查询条件(就是几个字符),就下载查询完的页面,由于需要查询的量较大,所以要写个程序来搞,现在困扰小弟的问题就是实现POST,以下是小弟的代码,望大虾指正
/*
* myLoader.java
*
* Created on 2005年12月26日, 下午2:02
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*//**
*
* @author ki
*/
import java.net.*;
import java.io.*;
public class myLoader {
String argString;
String message;
String post="POST";
public void setMessage(String msg){
this.message=msg;
}
public InputStream sendPostMessage()
throws IOException
{
URL url=new URL(argString);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod(post);
con.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
con.setRequestProperty("Content-type","multipart/form-data");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
OutputStream out = con.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(out,"ASCII");
osw.write(message);
osw.flush();
osw.close();
return con.getInputStream();
}
public String getText()
// 一个public方法,返回字符串,错误则返回"error open url"
{
try{
InputStream in=this.sendPostMessage();
InputStreamReader r=new InputStreamReader(in);
LineNumberReader din=new LineNumberReader(r);
String line="";
StringBuffer sb=new StringBuffer();
while((line=din.readLine())!=null)
{
sb.append(line+"\n");
din.skip(0);
}
return sb.toString();}
catch(Exception e){
return e.toString();}
}
/** Creates a new instance of myLoader */
public myLoader(String abc) {
this.argString=abc;
}
public static void main(String args[]){
String aaa="http://localhost/aaa/index.asp";
String msg="bushiba";
myLoader ml=new myLoader(aaa);
ml.setMessage(msg);
System.out.println(ml.getText());
}
}
/*
* myLoader.java
*
* Created on 2005年12月26日, 下午2:02
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*//**
*
* @author ki
*/
import java.net.*;
import java.io.*;
public class myLoader {
String argString;
String message;
String post="POST";
public void setMessage(String msg){
this.message=msg;
}
public InputStream sendPostMessage()
throws IOException
{
URL url=new URL(argString);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod(post);
con.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
con.setRequestProperty("Content-type","multipart/form-data");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
OutputStream out = con.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(out,"ASCII");
osw.write(message);
osw.flush();
osw.close();
return con.getInputStream();
}
public String getText()
// 一个public方法,返回字符串,错误则返回"error open url"
{
try{
InputStream in=this.sendPostMessage();
InputStreamReader r=new InputStreamReader(in);
LineNumberReader din=new LineNumberReader(r);
String line="";
StringBuffer sb=new StringBuffer();
while((line=din.readLine())!=null)
{
sb.append(line+"\n");
din.skip(0);
}
return sb.toString();}
catch(Exception e){
return e.toString();}
}
/** Creates a new instance of myLoader */
public myLoader(String abc) {
this.argString=abc;
}
public static void main(String args[]){
String aaa="http://localhost/aaa/index.asp";
String msg="bushiba";
myLoader ml=new myLoader(aaa);
ml.setMessage(msg);
System.out.println(ml.getText());
}
}
下面是打印到屏幕上import java.net.*;
import java.io.*;
import java.util.*;
public class GetPage {
public static void main(String[] args) throws Exception{
getContentByLanguage("zh-cn"); // 获取中文界面,可以更改
System.out.println("\n");
}
public static void getContentByLanguage(String country) throws Exception{
URL urlGoogle = new URL("http://www.google.com");
HttpURLConnection googleConnection =
(HttpURLConnection)urlGoogle.openConnection();
googleConnection.setRequestProperty("Accept-Language",country);
//googleConnection.setRequestProperty("Content-Range","Bytes 300-648/648");
googleConnection.setRequestProperty("Range","bytes=300-");
Map requests = googleConnection.getRequestProperties();
Set reqFields = requests.keySet();
Iterator itrReq =reqFields.iterator();
while(itrReq.hasNext()){
String field =(String)itrReq.next();
System.out.println(field+": "+
googleConnection.getRequestProperty(field));
}
googleConnection.connect();
System.out.println("\nResponse:");
Map response = googleConnection.getHeaderFields();
Set resFields = response.keySet();
Iterator itrRes =resFields.iterator();
while(itrRes.hasNext()){
String field =(String)itrRes.next();
System.out.println(field+": "+
googleConnection.getHeaderField(field));
}
System.out.println("\ngetContentLength():"+googleConnection.getContentLength()+"\n");
InputStream is = googleConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String strLine=null;
while((strLine=br.readLine())!=null){
System.out.println(strLine);
}
br.close();
googleConnection.disconnect();
}
}
例如:GET /articles/news/today.asp HTTP/1.1
Accept: */*
Accept-Language: en-us
Connection: Keep-Alive
Host: localhost
Referer: http://localhost/links.asp
User-Agent: Mozilla/4.0(compatible; MSIE5.5;Windows NT 5.0)
Accept-Encoding: gzip,deflate用空行分割消息头和实体内容。HTTP 响应消息一个完整的响应消息包括:一个状态行、若干个消息头、以及实体内容例如:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu,13 Jul 2000 05:46:53 GMT
Content-Length: 2291
Content-Type: text/html
Set-Cokies:
Cache-control: private<HTML>
<BODY>
......1、消息头大小写不敏感。
了解几个HTTP消息头1、Connection
用于指定处理完本次请求/响应后,客户端和服务器是否继续保持连接。设置值可以为:Keep-Alive(HTTP/1.1协议默认) 和 close
2、Accept-Language
用于指出客户机期望服务器返回的文档所使用的国家语言,可以指定多个以逗号分割的国家语言
3、Content-Length
用于表示实体内容的长度(字节数)
4、Range
用于指定服务器只需返回文档中的部分内容及内容范围:有以下几种使用格式:
1)Range: bytes=100-599
2)Range: bytes=100-
3)Range: bytes=-100
5、Content-Range(和Range配对使用)
用于指定服务器返回的部分实体内容的位置信息,例如:
Content-Range: Bytes 2543-4532/7898
确实很诱惑,所以我就赶来了. : )
比如你要查String str = "Hello World";
可以使用下面的请求:String req = "http://www.google.com/search?hl=zh-CN&q="+str + "&lr=";URL urlGoogle = new URL(req);即:http://www.google.com/search?hl=zh-CN&q=Hello+World&lr="当然req 要注意URL编码格式.以上只是通略讲法.
不是我懒的动脑子,只是哎~~小弟实在太菜,还望指教
MSN: [email protected]