用httpclient包写了个开心农场助手但不知道为什么那边的服务器就是不返回数据Body中没有JSON数据当然也不回有任何的效果但是明明是已经登陆上了农场 而且 操作时的farmkey也对 本人是菜鸟丢高手指教
 
下面是的简单的代码实现:
public class Farm {
public static void main(String[] args) throws Exception{

     HttpClient client = new HttpClient();
     
     /////////////////////////////////////////////////////
     ///////////////////登陆校内//////////////
     //////////////////////////////////////////////////
     ///////////////////////////////////////////////////
     
     PostMethod post = new PostMethod("http://www.renren.com/Login.do");

     NameValuePair value1= new NameValuePair("email","xxxxxxxxx");
     NameValuePair value2 = new NameValuePair("password","xxxxxxx");
     
     post.setRequestBody(new NameValuePair[]{value1,value2});
     
     HttpMethod submitXiaoNei = (HttpMethod)post;
     
     client.executeMethod(submitXiaoNei);
     
      submitXiaoNei.releaseConnection();
     
    //GetMethod xiaoNeiHome = new GetMethod("http://home.renren.com/Home.do");
    
     
  //client.executeMethod((HttpMethod)xiaoNeiHome);
     
     //xiaoNeiHome.releaseConnection();
   /////////////////////////////////////////////////////
     ///////////////////登陆校内//////////////
     //////////////////////////////////////////////////
     ///////////////////////////////////////////////////
     
  
  //////////////登陆开心农场////////////
     
     
     List<Header> headers = new ArrayList<Header>();  
     
     headers.add(new Header("Connection","Keep-Alive"));
     
     headers.add(new Header("Content-Type","application/x-www-form-urlencoded"));
     
     headers.add(new Header("Accept","*/*"));
     
     headers.add(new Header("Accept-Encoding","identity"));
     
     headers.add(new Header("Accept-Language","zh-CN"));
     
     headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
     
     client.getHostConfiguration().getParams().setParameter("http.default-headers",headers);  
     
     
     
  GetMethod method3 = new GetMethod("http://apps.renren.com/happyfarm");
 
    client.executeMethod((HttpMethod)method3);
    
    byte[] b=  method3.getResponseBody();
      
   String string= new String(b);
   
   int number1 =string.lastIndexOf("<iframe name=\"iframe_canvas\" id=\"iframe_canvas\" src=\"")+53;
   
   int number2 =string.lastIndexOf("frameborder")-2; ////////获得开心农场资源连接的办法很笨不要笑我!!!
   
   
   
   String farmlan1= string.substring(number1,number2);
   
   String farmlan2=farmlan1.replace("amp;","");
    
   System.out.println(farmlan2);
    
 
      client.getState().clearCookies();
    
     String farmtime=String.valueOf(System.currentTimeMillis()).substring(0,10);
     
     String farmkey= getMD5Str(farmtime+"15l3h4kh");
     GetMethod submitFarm = new GetMethod(farmlan2);
     
      client.executeMethod(submitFarm);
     
    
     
       PostMethod action1 = new PostMethod("http://xn.hf.fminutes.com/api.php?mod=user&act=run&flag=1&farmKey="+farmkey+"&farmTime="+farmtime+"&inuId=");  
    
   NameValuePair value3= new NameValuePair("ownerId","239346676");   ////////切到ID为23934667好友的页面的操作//////////
  
    
   action1.setRequestBody(new NameValuePair[]{value3});
    
    client.executeMethod(action1);
    
     action1.releaseConnection();
   
     
}
/////////////////////MD5值计算函数/////////////////////
  private static String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
  
        try {  
            messageDigest = MessageDigest.getInstance("MD5");  
  
            messageDigest.reset();  
  
            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        byte[] byteArray = messageDigest.digest();  
  
        StringBuffer md5StrBuff = new StringBuffer();  
  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            else  
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
        }  
  
        return md5StrBuff.toString();  
    }  
}

解决方案 »

  1.   


    这是校内网上的开心农场 登陆时没有验证码 而且通过服务器返回的HTML来看的确是是登陆成功了!!
      

  2.   

    client.getHostConfiguration().getParams().setParameter("http.default-headers",headers);

    client.getState().clearCookies(); 报错,说是没有定义getParams方法和clearCookies方法
      

  3.   

    虽然看到贴子是好几个月前的了,但是自己试了下,常量密钥“15l3h4kh”竟然还有效,我刚才试的:
    这是源请求头,
    POST /api.php?mod=user&act=run&flag=1&farmKey=e4800b55f34e2d7691cde46f023dd849&farmTime=1263782928&inuId= HTTP/1.1然后我拿 MD5.hash("1263782928"+"15l3h4kh")确实等于e4800b55f34e2d7691cde46f023dd849请问你的1513h4kh是怎么得到的?