在web应用程序中,一般都是通过页面发送http的post请求,但也可以使用java程序来模拟页面发送请求,代码如下:
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.URL;
- import java.net.URLConnection;
- public class TestPost {
- public static void testPost() throws IOException {
-
-
-
-
-
-
-
-
-
-
-
-
- URL url = new URL("http://www./message/check.asp");
- URLConnection connection = url.openConnection();
-
-
-
-
- connection.setDoOutput(true);
-
-
-
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
- out.write("username=kevin&password=*********");
-
- out.flush();
- out.close();
-
-
-
-
-
-
-
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- InputStream l_urlStream;
- l_urlStream = connection.getInputStream();
-
- BufferedReader l_reader = new BufferedReader(new InputStreamReader(
- l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- sTotalString += sCurrentLine + "/r/n";
-
- }
- System.out.println(sTotalString);
- }
-
- public static void main(String[] args) throws IOException {
- testPost();
- }
- }
这个程序我已经测试通过的。
如果除了post一些数据外,还要上传文件,可以使用下面两个函数之一:
这两个函数是公司的员工写的,还没有用实际的例子测试过。
我们还可以使用htmlparse的jar包(该包及相关文档可以在http://htmlparser./这里下载)提供的函数对获取的html进行解析 例子如下:
- import org.htmlparser.Node;
- import org.htmlparser.NodeFilter;
- import org.htmlparser.Parser;
- import org.htmlparser.filters.TagNameFilter;
- import org.htmlparser.tags.TableTag;
- import org.htmlparser.util.NodeList;
- public class TestHTMLParser {
- public static void testHtml() {
- try {
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- java.io.InputStream l_urlStream;
- java.net.URL l_url = new java.net.URL("http://www./html/doc/2006/07/04/00929.html");
- java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
- l_connection.connect();
- l_urlStream = l_connection.getInputStream();
- java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- sTotalString += sCurrentLine+"/r/n";
-
- }
- String testText = extractText(sTotalString);
- System.out.println( testText );
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- public static String extractText(String inputHtml) throws Exception {
- StringBuffer text = new StringBuffer();
- Parser parser = Parser.createParser(new String(inputHtml.getBytes(),"GBK"), "GBK");
-
- NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
- public boolean accept(Node node) {
- return true;
- }
- });
-
- System.out.println(nodes.size());
- for (int i=0;i<nodes.size();i++){
- Node nodet = nodes.elementAt(i);
-
- text.append(new String(nodet.toPlainTextString().getBytes("GBK"))+"/r/n");
- }
- return text.toString();
- }
-
- public static void test5(String resource) throws Exception {
- Parser myParser = new Parser(resource);
- myParser.setEncoding("GBK");
- String filterStr = "table";
- NodeFilter filter = new TagNameFilter(filterStr);
- NodeList nodeList = myParser.extractAllNodesThatMatch(filter);
- TableTag tabletag = (TableTag) nodeList.elementAt(11);
-
- }
-
- public static void main(String[] args) throws Exception {
-
- testHtml();
- }
- }
这个例子也已经测试通过。
转:http://blog.csdn.net/caihaijiang/article/details/5903133
|