wingrove 2013-12-13 05:52 采纳率: 0%
浏览 15502
已采纳

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 }

  • 写回答

1条回答

  • 上官竹 2014-01-16 06:48
    关注

    比如你有一个页面index.html,这个页面有一个文本域,名称是:a,值是123,你用get提交后,你的url会变成:index.html?a=123,在获取的时候,就是从浏览器中回去参数了,而不是表单!
    由于是这样,get方式提交表单,参数有限,不能提交大量数据,而且安全性不高!
    用post方式提交,就不会使用url传参数,而且数据量几乎没有限制,这样可以提交大量的数据,安全性可靠!(拾人牙慧)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面