httpClient使用postMethod请求

(一) maven环境下引入必备jar

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

(二) 使用httpClient 获取登录的cookie并携带cookie发起请求

//导入的jar包
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.HttpException;

  org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
    httpClient.getHostConfiguration().setProxy("localhost",8888);//设置本地代理,方便fiddler监听
    //使用httpClient登录网站,并获取cookie
    PostMethod postMethod = new PostMethod("loginUrl");
    //使用NameValuePair 传递参数
    PostMethod method = new PostMethod("登录后请求的地址"); 
    NameValuePair[] data = { new NameValuePair("key","value") };
    postMethod.setRequestBody(data);

     // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

     // 获取请求的cookie
    Cookie[] cookies = httpClient.getState().getCookies();
    StringBuffer tmpcookies = new StringBuffer();
            for (Cookie c : cookies) {
                tmpcookies.append(c.toString() + ";");
            }
    //携带请求的cookie
     method.setRequestHeader("cookie",tmpcookies.toString());

    //发起请求
    int executeMethod = httpClient.executeMethod(method);

    //获取请求头
    org.apache.commons.httpclient.Header[] headers = method.getResponseHeaders();

    //获取响应,postMethod 提供两种方式,getResponseBodyAsString和getResponseBodyAsStream,但前者在没指定Content-length或者Content-length过长时会报错,推荐使用后者.
    InputStream stream = method.getResponseBodyAsStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(stream));  
         StringBuffer stringBuffer = new StringBuffer(); 
         String str1= "";  
         while((str1 = br.readLine()) !=null){  
         stringBuffer.append(str1);  
         }  
        String sb=stringBuffer.toString();
        //按照utf-8格式输出响应体
        String result = new String(sb.getBytes(),"UTF-8");
        JSONArray fromObject = JSONArray.fromObject(result);
        JSONObject json = fromObject.getJSONObject(0);
        logger.info(result);

(三)使用时遇到的一些坑

  • 使用postMethod访问登陆的url获取的cookie有问题,导致后期通过这个cookie请求数据失效,后来通过fiddler抓包看到了正确的登陆请求地址。fiddler大法好。
  • 在postMethod请求携带cookie信息时候,之前使用如下方式意图携带cookie访问,实测无效。
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("key","value");
//请求后获取得到的cookie1,由于包不同,不能直接直接执行cookieStore.addCookie(cookie1)
//之前妄图通过BasicClientCookie cookie = new BasicClientCookie(cookie1.getName,cookie1.getValue());来强转换,实测不行。。。
//携带cookie信息还是要通过设置请求头信息来
cookieStore.addCookie(cookie);
httpClient.setCookieStore(cookieStore);        
httpClient.execute("method");
解决发起post请求时候中文乱码问题
//第一种方式        
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");//解决中文乱码 

//第二种方式,通过重载getRequestCharSet()方法设置提交的编码(字符集)。 
public class UTF8PostMethod extends PostMethod {
    public UTF8PostMethod(String url){       
        super(url);       
        }       
        @Override       
        public String getRequestCharSet() {       
            //return super.getRequestCharSet();       
            return "UTF-8";       
        }    
}

//两种方式都实测有效。
发布了23 篇原创文章 · 获赞 6 · 访问量 3万+
展开阅读全文

java中PostMethod和HttpPost都可以提交post表单,请问他们有什么区别

12-13

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<String, String> 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 } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览