最近在写个淘宝相关的程序,一向很熟悉httpWebRequest的,却在程序中浪费了很多时间,呵呵,还是不够细心啊。虽然TopApi很好很方便,但是对于非商城类用户,想接入并投入使用真不容易,所以这里给个登淘宝的关键代码,希望能帮到您。。。。
public static string getSaleInfo(string User, string psw, string VCode, int myIndex) { string tmpUser = myEncoding(User, 1); string tmpPsw = psw; string tmpVCode = VCode; if (tmpToken == "") return "初始化登录失败"; string strData = string.Format("TPL_username={0}&TPL_password={1}"+ "&action=Authenticator&event_submit_do_login=anything&TPL_redirect_url=http%3A%2F%2Fwww.taobao.com&from=tb&fc=2&style=" + "default&css_style=&tid=XOR_1_000000000000000000000000000000_6358305443087C737B03067F" + "&support=000001&CtrlVersion=1%2C0%2C0%2C7&loginType=3&minititle=&minipara=&pstrong=1&longLogin=-1&llnick=" + "&sign=&need_sign=&isIgnore=&popid=&callback=&guf=¬_duplite_str=&need_user_id=&poy=&gvfdcname=10&" + "from_encoding=TPL_username={0}&TPL_password={1}&_tb_token_={2}&action=Authenticator&" + "event_submit_do_login=anything&TPL_redirect_url=http%3A%2F%2Fwww.taobao.com%2F&from=tb&fc=2&style=default&css_style=&tid=XOR_1_000000000000000000000000000000_6358305443087C737B03067F" + "&support=000001&CtrlVersion=1%2C0%2C0%2C7&loginType=3&minititle=&minipara=&pstrong=1&longLogin=-1&llnick=&sign=&need_sign=&isIgnore=&popid=&callback=&guf=" + "¬_duplite_str=&need_user_id=&poy=&gvfdcname=10&from_encoding=", tmpUser, tmpPsw, tmpToken); string strUrl1 = "https://login.taobao.com/member/login.jhtml"; string tmpStr = Execute(strUrl1, "post", strData, "gb2312"); // 获取投诉信息 tmpStr = Execute(strUrl, "get", "", "gb2312"); int iComplain = 0; string tmpComplain = myMatch(tmpStr, "(?<=收到投诉\\().*(?=\\))", 0); try { iComplain = int.Parse(tmpComplain); } catch { } // 获取卖家信息 strUrl = "http://i.taobao.com/my_taobao.htm?seller=true"; tmpStr = Execute(strUrl, "get", "", "gb2312"); string tmpWaitSend = myMatch(tmpStr, "(?待发货订单\\().*(?=\\))", 0); string tmpRefunding = myMatch(tmpStr, "(?退款中订单\\().*(?=\\))", 0); int iRefunding = 0; int iWaitSend = 0; try { iWaitSend = int.Parse(tmpWaitSend); } catch { } try { iRefunding = int.Parse(tmpRefunding); } catch { } app.myReport[myIndex].User = User; app.myReport[myIndex].iWaitSend = iWaitSend; app.myReport[myIndex].iExitFund = iRefunding; app.myReport[myIndex].iCompain = iComplain; return "getOK"; }
重要提示:注意登录URL的顺序很重要,链接换了就可能不工作了。代码中包含了登录表单数据的构造、POST请求发送、以及从淘宝个人中心页面提取关键业务数据(待发货订单数、退款中订单数、投诉数等)的完整流程。
需要正确处理淘宝的登录Token验证机制
登录表单数据格式需要严格按照淘宝要求构造
字符编码使用GB2312
通过正则表达式从HTML页面中提取所需数据
需要处理各种异常情况,如数据解析失败等