HttpPost获取淘宝的历史订单
            HttpPost httpost2 = new HttpPost("http://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/QueryAction&event_submit_do_query=1&_input_charset=utf8");
            // All the parameters post to the web site
            List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
            nvps2.add(new BasicNameValuePair("pageNum", "1"));
            nvps2.add(new BasicNameValuePair("pageSize", "20"));
            try {
                httpost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
                response = httpclient.execute(httpost2);
                System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                printError("登录失败");
                e.printStackTrace();

            } finally {
                httpost2.abort();
            }

已经登陆成功了,现在要获取历史订单,可是为什么这个返回的是空的。

4个回答

你这代码能登录成功?没看到用户名、密码、token和cookie,根本就没有登录嘛。

chenxinjie005
chenxinjie005 回复showbo: 还是不能换页取值
大约一年之前 回复
chenxinjie005
chenxinjie005 回复chenxinjie005: 你Q多少请教一下
大约一年之前 回复
chenxinjie005
chenxinjie005 回复showbo: 我看了post请求啊,我也是用post请求。我用抓包工具,也看见参数传进去了。
大约一年之前 回复
showbo
支付宝加好友偷能量挖 回复chenxinjie005: 注意请求方式,不是添加不添加的问题,是post还是get,自己开发工具看
大约一年之前 回复
chenxinjie005
chenxinjie005 回复showbo: nvps2.add(new BasicNameValuePair("pageNum", "1")); nvps2.add(new BasicNameValuePair("pageSize", "20")); 我有添加参数啊
大约一年之前 回复
showbo
支付宝加好友偷能量挖 回复chenxinjie005: 浏览器开发工具看请求时post还是get发送的分页参数,这些不能少的,如果还返回了token之类的防止crsf的数据,下次请求要附带上token值
大约一年之前 回复
chenxinjie005
chenxinjie005 回复showbo: 添加了Referer可以返回了,可是为什么设置查询页数和页码条件没起作用,一直都是第一页15条
大约一年之前 回复
showbo
支付宝加好友偷能量挖 回复chenxinjie005: 没设置来源页面吧。。Referer要加上,为请求接口的页面地址
大约一年之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复chenxinjie005: 你怎么确认你登录进去的?既然你不愿意贴出代码。那么你抓包看下。你登录进去应该返回cookie,这个有没有带上。
大约一年之前 回复
chenxinjie005
chenxinjie005 我登录的代码没贴出来,我已经登陆进去,现在要获取账户的历史订单出现问题
大约一年之前 回复

回复caozhy: package com.thinkgem.jeesite.common.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class HttpClientUtil { private String userName = ""; private String password = ""; String tmpcookies = ""; private static String redirectURL = "http://i.taobao.com/my_taobao.htm"; private static String loginURL = "https://login.taobao.com/member/login.jhtml"; private HttpResponse response; private DefaultHttpClient httpclient = null; private String redirectLocation = null; private String defaultContent = null; public HttpClientUtil(String userName, String password) { this.userName = userName; this.password = password; } public void printInfo(String info){ System.out.println(info); } public void printError(String info){ System.out.println(info); } @SuppressWarnings("deprecation") public boolean login() { printInfo("开始登陆。。。"); if (httpclient != null) { System.out.println("已经登陆成功"); return true; } httpclient = null; httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(loginURL); // All the parameters post to the web site List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("origURL", redirectURL)); nvps.add(new BasicNameValuePair("autoLogin", "true")); nvps.add(new BasicNameValuePair("TPL_username", userName)); nvps.add(new BasicNameValuePair("TPL_password", password)); try { httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(httpost); } catch (Exception e) { printError("登录失败"); e.printStackTrace(); return false; } finally { httpost.abort(); } Header[] hs = response.getAllHeaders(); for (Header h : hs) { // System.out.println(h.getName() + ":" + h.getValue()); printInfo(h.getName() + ":" + h.getValue()); } redirectLocation = getRedirectLocation(); redirectLocation = "http://i.taobao.com/my_taobao.htm"; if (getToken() == null) { System.out.println("登录失败"); return false; } List cookies = httpclient .getCookieStore().getCookies();// 取出登陆成功后,服务器返回的cookies信息,里面保存了服务器端给的“临时证” tmpcookies = ""; for (int i = 0; i < cookies.size(); i++) { org.apache.http.cookie.Cookie c = cookies.get(i); tmpcookies = tmpcookies + c.getName() + "=" + c.getValue() + ";"; // System.out.println(c); } printInfo("完成登陆"); return true; } private String getRedirectLocation() { Header locationHeader = response.getFirstHeader("Location"); if (locationHeader == null) { return null; } return locationHeader.getValue(); } public String getToken() { String retu = getText(redirectLocation); String requesttoken = null; int start = retu.indexOf("已买到的宝贝");// 这里要改一下,看一下登录成功后页面有什么内容,以区分是否成功登录,以前淘宝登录后可以使用"查看余额"区分,貌似现在不行了 if (start != -1) { requesttoken = "success"; defaultContent = retu; } return requesttoken; } private String getText(String redirectLocation) { HttpGet httpget = new HttpGet(redirectLocation); // Create a response handler ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = ""; try { responseBody = httpclient.execute(httpget, responseHandler); } catch (Exception e) { e.printStackTrace(); responseBody = null; } finally { httpget.abort(); } // System.out.println(responseBody); return responseBody; } public String getDefaultContent() { return defaultContent; } @SuppressWarnings("deprecation") public boolean logout() { printInfo("开始退出。。。"); if (login()) { String logoutUrl = "退出登录的url"; HttpPost httpost = new HttpPost(logoutUrl); List nvps2 = new ArrayList(); try { httpost.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8)); response = httpclient.execute(httpost); // System.out.println("退出状态 " + response.getStatusLine()); httpost.abort(); } catch (Exception e) { printError("退出失败"); e.printStackTrace(); return false; } finally { httpost.abort(); httpclient.getConnectionManager().shutdown(); } } printInfo("完成退出"); return true; } public String getBoughtList() { printInfo("开始截取[已买到的宝贝]页面数据。。。"); HttpPost httpost2 = new HttpPost("http://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/QueryAction&event_submit_do_query=1&_input_charset=utf8"); // All the parameters post to the web site List nvps2 = new ArrayList(); nvps2.add(new BasicNameValuePair("pageNum", "1")); nvps2.add(new BasicNameValuePair("pageSize", "20")); try { httpost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8)); httpost2.setHeader("cookie", tmpcookies); response = httpclient.execute(httpost2); System.out.println(EntityUtils.toString(response.getEntity())); } catch (Exception e) { printError("登录失败"); e.printStackTrace(); } finally { httpost2.abort(); } printInfo("完成截取[已买到的宝贝]页面数据"); return ""; } public String getUrlContent(String url) { printInfo("开始截取菜单项页面数据。。。"); HttpGet httpget = new HttpGet( url); httpget.setHeader("cookie", tmpcookies); // Create a response handler ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = null; try { responseBody = httpclient.execute(httpget, responseHandler); } catch (ClientProtocolException e) { printError("截取页面数据失败"); e.printStackTrace(); } catch (IOException e) { printError("截取页面数据失败"); e.printStackTrace(); } finally { httpget.abort(); } printInfo("完成截取菜单项页面数据"); return responseBody; } public static void main(String[] args) { HttpClientUtil taobao = new HttpClientUtil("xinredl", "cxq6304697"); taobao.login(); System.out.println(taobao.getBoughtList()); // HtmlParserUtil h = new HtmlParserUtil(); // h.parseBoughtList(taobao.getBoughtList()); // taobao.logout(); } } 这个所有的代码

package com.thinkgem.jeesite.common.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {
private String userName = "";

    private String password = "";

    String tmpcookies = "";

    private static String redirectURL = "http://i.taobao.com/my_taobao.htm";

    private static String loginURL = "https://login.taobao.com/member/login.jhtml";

    private HttpResponse response;

    private DefaultHttpClient httpclient = null;

    private String redirectLocation = null;

    private String defaultContent = null;

    public HttpClientUtil(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public void printInfo(String info){
        System.out.println(info);
    }

    public void printError(String info){
        System.out.println(info);
    }

    @SuppressWarnings("deprecation")
    public boolean login() {
        printInfo("开始登陆。。。");
        if (httpclient != null) {
            System.out.println("已经登陆成功");
            return true;
        }
        httpclient = null;
        httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(loginURL);
        // All the parameters post to the web site
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("origURL", redirectURL));
        nvps.add(new BasicNameValuePair("autoLogin", "true"));
        nvps.add(new BasicNameValuePair("TPL_username", userName));
        nvps.add(new BasicNameValuePair("TPL_password", password));
        try {
            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            response = httpclient.execute(httpost);
        } catch (Exception e) {
            printError("登录失败");
            e.printStackTrace();
            return false;
        } finally {
            httpost.abort();
        }
        Header[] hs = response.getAllHeaders();
        for (Header h : hs) {
            // System.out.println(h.getName() + ":" + h.getValue());
            printInfo(h.getName() + ":" + h.getValue());
        }
        redirectLocation = getRedirectLocation();
        redirectLocation = "http://i.taobao.com/my_taobao.htm";
        if (getToken() == null) {
            System.out.println("登录失败");
            return false;
        }
        List<org.apache.http.cookie.Cookie> cookies = httpclient
                .getCookieStore().getCookies();// 取出登陆成功后,服务器返回的cookies信息,里面保存了服务器端给的“临时证”
        tmpcookies = "";
        for (int i = 0; i < cookies.size(); i++) {
            org.apache.http.cookie.Cookie c = cookies.get(i);
            tmpcookies = tmpcookies + c.getName() + "=" + c.getValue() + ";";
            // System.out.println(c);
        }
        printInfo("完成登陆");
        return true;
    }

    private String getRedirectLocation() {
        Header locationHeader = response.getFirstHeader("Location");
        if (locationHeader == null) {
            return null;
        }
        return locationHeader.getValue();
    }

    public String getToken() {
        String retu = getText(redirectLocation);
        String requesttoken = null;
        int start = retu.indexOf("已买到的宝贝");// 这里要改一下,看一下登录成功后页面有什么内容,以区分是否成功登录,以前淘宝登录后可以使用"查看余额"区分,貌似现在不行了
        if (start != -1) {
            requesttoken = "success";
            defaultContent = retu;
        }
        return requesttoken;
    }

    private String getText(String redirectLocation) {
        HttpGet httpget = new HttpGet(redirectLocation);
        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = "";
        try {
            responseBody = httpclient.execute(httpget, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
            responseBody = null;
        } finally {
            httpget.abort();
        }
        // System.out.println(responseBody);
        return responseBody;
    }

    public String getDefaultContent() {
        return defaultContent;
    }

    @SuppressWarnings("deprecation")
    public boolean logout() {
        printInfo("开始退出。。。");
        if (login()) {
            String logoutUrl = "退出登录的url";
            HttpPost httpost = new HttpPost(logoutUrl);
            List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
            try {
                httpost.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
                response = httpclient.execute(httpost);
                // System.out.println("退出状态 " + response.getStatusLine());
                httpost.abort();
            } catch (Exception e) {
                printError("退出失败");
                e.printStackTrace();
                return false;
            } finally {
                httpost.abort();
                httpclient.getConnectionManager().shutdown();
            }
        }
        printInfo("完成退出");
        return true;
    }

    public String getBoughtList() {
        printInfo("开始截取[已买到的宝贝]页面数据。。。");
         HttpPost httpost2 = new HttpPost("http://buyertrade.taobao.com/trade/itemlist/asyncBought.htm?action=itemlist/QueryAction&event_submit_do_query=1&_input_charset=utf8");
        // All the parameters post to the web site
        List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
        nvps2.add(new BasicNameValuePair("pageNum", "1"));
        nvps2.add(new BasicNameValuePair("pageSize", "20"));
        try {
            httpost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
            httpost2.setHeader("cookie", tmpcookies);
            response = httpclient.execute(httpost2);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            printError("登录失败");
            e.printStackTrace();

        } finally {
            httpost2.abort();
        }
        printInfo("完成截取[已买到的宝贝]页面数据");
        return "";
    }

    public String getUrlContent(String url) {
        printInfo("开始截取菜单项页面数据。。。");
        HttpGet httpget = new HttpGet(
                url);
        httpget.setHeader("cookie", tmpcookies);
        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = null;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            printError("截取页面数据失败");
            e.printStackTrace();
        } catch (IOException e) {
            printError("截取页面数据失败");
            e.printStackTrace();
        } finally {
            httpget.abort();
        }

        printInfo("完成截取菜单项页面数据");
        return responseBody;
    }

     public static void main(String[] args) {
         HttpClientUtil taobao = new HttpClientUtil("xinredl", "cxq6304697");
     taobao.login();
     System.out.println(taobao.getBoughtList());
   //  HtmlParserUtil h = new HtmlParserUtil();
    // h.parseBoughtList(taobao.getBoughtList());
     // taobao.logout();
     }

}

用python,或者用你的java调用python https://blog.csdn.net/liujiayu2/article/details/79385958

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!