博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpPost works in Java project, not in Android
阅读量:4975 次
发布时间:2019-06-12

本文共 3862 字,大约阅读时间需要 12 分钟。

publicstaticMyBean gatherData(String username,String password){
    MyBean myBean =newMyBean();     try{
        HttpResponse response = doHttpGet(URL_PAGE_LOGIN,null,null);         System.out.println("Got login page");         String content =EntityUtils.toString(response.getEntity());         String token =ContentParser.getToken(content);         String cookie = getCookie(response);         System.out.println("Performing login");         System.out.println("token = "+token +" || cookie = "+cookie);         response = doLoginPost(username,password,cookie, token);         int respCode = response.getStatusLine().getStatusCode();         if(respCode !=302){
            System.out.println("ERROR: not a 302 redirect!: code is \""+ respCode+"\"");             if(respCode ==200){
                System.out.println(getHeaders(response));                 System.out.println(EntityUtils.toString(response.getEntity()).substring(0,500));             }         }else{
            System.out.println("Logged in OK, loading account home");             // redirect handler and rest of parse removed         }     }catch(Exception e){
        System.out.println("ERROR in gatherdata: "+e.toString());         e.printStackTrace();     }     return myBean; } privatestaticHttpResponse doHttpGet(String url,String cookie,String referrer){
    try{
        HttpClient client =newDefaultHttpClient();         client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);         client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");         HttpGet httpGet =newHttpGet(url);         httpGet.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);         httpGet.setHeader(HEADER_USER_AGENT,HEADER_USER_AGENT_VALUE);         if(referrer !=null&&!referrer.equals("")) httpGet.setHeader(HEADER_REFERER,referrer);         if(cookie !=null&&!cookie.equals("")) httpGet.setHeader(HEADER_COOKIE,cookie);         return client.execute(httpGet);     }catch(Exception e){
        e.printStackTrace();         thrownewConnectException("Failed to read content from response");     } } privatestaticHttpResponse doLoginPost(String username,String password,String cookie,String token)throwsClientProtocolException,IOException{
    try{
        HttpClient client =newDefaultHttpClient();         client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);         client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");         HttpPost post =newHttpPost(URL_LOGIN_SUBMIT);         post.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);         post.setHeader(HEADER_USER_AGENT,HEADER_USER_AGENT_VALUE);         post.setHeader(HEADER_REFERER, URL_PAGE_LOGIN);         post.setHeader(HEADER_COOKIE, cookie);         post.setHeader("Content-Type","application/x-www-form-urlencoded");         List
formParams =newArrayList
();         formParams.add(newBasicNameValuePair("org.apache.struts.taglib.html.TOKEN", token));         formParams.add(newBasicNameValuePair("showLogin","true"));         formParams.add(newBasicNameValuePair("upgrade",""));         formParams.add(newBasicNameValuePair("username", username));         formParams.add(newBasicNameValuePair("password", password));         formParams.add(newBasicNameValuePair("submit","Secure+Log+in"));         UrlEncodedFormEntity entity =newUrlEncodedFormEntity(formParams,HTTP.UTF_8);         post.setEntity(entity);         return client.execute(post);     }catch(Exception e){
        e.printStackTrace();         thrownewConnectException("ERROR in doLoginPost(): "+e.getMessage());     } }

转载于:https://www.cnblogs.com/myphoebe/archive/2012/01/06/2314847.html

你可能感兴趣的文章
旋转菜单
查看>>
Masonry介绍与使用实践(快速上手Autolayout)(转)
查看>>
hihoCoder #1770 : 单调数(数位dp)
查看>>
友情链接
查看>>
laravel入门-CSRF解决
查看>>
数据库 chapter 17 数据仓库与联机分析处理技术
查看>>
Hdu4547CD操作离线lca
查看>>
jquery的基本事件大全
查看>>
git打tag
查看>>
Docker容器中安装vim
查看>>
前言:学习自动化之前需要知道的
查看>>
HTML5 - Canvas动画样例(谷歌弹跳球)
查看>>
Spring注解注入
查看>>
hdu 1045 Fire Net dfs深搜或者二分匹配
查看>>
sqlserver 时间转换
查看>>
多态、接口
查看>>
浅拷贝 深拷贝
查看>>
Linux系统部署samba服务记录
查看>>
bzoj 1068: [SCOI2007]压缩
查看>>
python检查是否是闰年
查看>>