程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> http-java中PostMethod和HttpPost都可以提交post表單,請問他們有什麼區別

http-java中PostMethod和HttpPost都可以提交post表單,請問他們有什麼區別

編輯:編程綜合問答
java中PostMethod和HttpPost都可以提交post表單,請問他們有什麼區別

PostMethod
HttpPost 尤其是在cookie和保存信息方面有什麼不同?求大神解釋
我拋磚引玉,上代碼!

import java.io.BufferedReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Byrbt {

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    //先下載登陸頁面
    URL login=new URL("http://bt.byr.cn/login.php");

    InputStreamReader isr=new InputStreamReader(login.openStream(),"utf-8");

    BufferedReader br=new BufferedReader(isr);

    String line="";

    String html="";

    while((line=br.readLine())!=null){
        //html=html+line+"\n";
        html+=line;
    }

    Document doc=Jsoup.parse(html);

    //找到圖片,下載圖片

    String imgSrc="";

    imgSrc=doc.body().getElementsByAttribute("method").select("tr").select("img").attr("src").toString();

    String realSrc="http://bt.byr.cn/"+imgSrc;

    System.out.println(realSrc);

    getPhotoFromWeb(realSrc);

    String imgString="";
    String imgHash="";

    imgHash=realSrc.split("imagehash=")[1];

    System.out.println("輸入d盤裡的驗證碼");
    Scanner sca =new Scanner(System.in);

    imgString=sca.next();

    //登陸

    HttpClient httpClient=new HttpClient();

    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    PostMethod post = new PostMethod("http://bt.byr.cn/login.php");
    post.setRequestHeader(new Header(
                    "User-Agent",
                    "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0"));
    post.addParameter("username", "wingrove");
    post.addParameter("password","jiao12" );
    post.addParameter("imagestring",imgString);
    post.addParameter("imagehash",imgHash);


    httpClient.executeMethod(post);

    System.out.println("GET POST STATUS: "+ post.getStatusLine().toString());

// GetMethod get = new GetMethod("http://bt.byr.cn/torrents.php?cat=406");
// get.setRequestHeader(new Header(
// "User-Agent",
// "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0"));

// httpClient.executeMethod(get);
//

isr=new InputStreamReader(post.getResponseBodyAsStream(),"utf-8");

    br=new BufferedReader(isr);

    line="";
    html="";

    while((line=br.readLine())!=null){
        html=html+line+"\n";
    }

    System.out.println(html);

}
public static void getPhotoFromWeb(String urlString) throws MalformedURLException, Exception{
    //這個程序只適用於網絡,不適用於本地可能是由於url的局限

    URL url = new URL(urlString);
    File outFile =new File("d:/CAPTCHA.jpg");
    OutputStream os = new FileOutputStream(outFile);
    InputStream is = url.openStream();
    byte[] buff = new byte[1024];
    while(true) {
        int readed = is.read(buff);
        if(readed == -1) {
            break;
        }
        byte[] temp = new byte[readed];
        System.arraycopy(buff, 0, temp, 0, readed);
        os.write(temp);
    }
    is.close(); 
    os.close();

}

}

private void doPost(String url, Map<String, String> headers, String body) throws IOException {

02 HttpPost post = new HttpPost(url);
03 for (Map.Entry header : headers.entrySet()) {
04 post.setHeader(header.getKey(), header.getValue());
05 }
06 post.setEntity(new StringEntity(body));
07 HttpContext context = createBasicAuthContext("admin", "password");
08 CloseableHttpResponse response = client.execute(host, post, context);
09 try {
10 // status = response.getStatusLine().getStatusCode();
11 // headers = response.getAllHeaders();
12

13 // HttpEntity entity = response.getEntity();
14 // text = IOUtils.toString(entity.getContent(), "ISO-8859-1");
15 } finally {
16 response.close();
17 }
18 }

最佳回答:


比如你有一個頁面index.html,這個頁面有一個文本域,名稱是:a,值是123,你用get提交後,你的url會變成:index.html?a=123,在獲取的時候,就是從浏覽器中回去參數了,而不是表單!
由於是這樣,get方式提交表單,參數有限,不能提交大量數據,而且安全性不高!
用post方式提交,就不會使用url傳參數,而且數據量幾乎沒有限制,這樣可以提交大量的數據,安全性可靠!(拾人牙慧)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved