通过Fiddler抓取到CSDN的Android客户端的请求接口后,在访问接口时,有些是不用登陆就可以查看的,而有些是需要登陆才能查看的,不需要登陆的接口,直接通过Http链接直接访问便可以拿到数据,

如查询我的博客的前20条博文的链接

http://ms.csdn.net/api/blog/user_blog_list?SessionId=IhO4aEgYT6HTOI%2FiWRVgl6rHVJHLQUKkQAIzJ1Be1zsXbYiSdAyKLGcjve5x%2BxmTrrN8sBjCE7CLok6tso2z3g%3D%3D&username=zml_2015&page=1&pagesize=20

就可以直接拿到数据,如下图:(在新标签中打开下面图片可查看大图)

https://img.alicdn.com/imgextra/i1/1830641203/TB2mXlQX1vB11BjSspnXXbE.pXa_!!1830641203.jpg

但是如果需要登陆才能访问到的接口,如

http://ms.csdn.net/api/blog/columnlist?SessionId=IhO4aEgYT6HTOI%2FiWRVgl6rHVJHLQUKkQAIzJ1Be1zsXbYiSdAyKLGcjve5x%2BxmTrrN8sBjCE7CLok6tso2z3g%3D%3D&page=1&size=20&username=zml_2015 

直接访问这个Http链接的时候,就会返回:

{  
code: 4001,  
message: "没有登录",  
data: null,  
sessionId: "",  
sessionExpired: "1474449502008"  
}  

解决方案:

 

在请求头中加两对参数:

                        platform: android
                        version: 1.9.6

下面给出一个Java的网络请求实例:

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

/**

  • @author zml2015

  • @Time:2016年9月19日 下午8:15:11

  • @version 1.0
    */
    public class Main {

    public static void main(String[] args) throws Exception {
    URL url = new URL(
    "http://ms.csdn.net/api/blog/columnlist?SessionId=IhO4aEgYT6HTOI%2FiWRVgl6rHVJHLQUKkQAIzJ1Be1zsXbYiSdAyKLGcjve5x%2BxmTrrN8sBjCE7CLok6tso2z3g%3D%3D&page=1&size=20&username=zml_2015");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("platform", "android");
    connection.setRequestProperty("version", "1.9.6");
    // connection.setRequestProperty("User-Agent",
    // "Dalvik/2.1.0 (Linux; U; Android 6.0; H60-L01 Build/HDH60-L01)");
    // connection.setRequestProperty("Host", "ms.csdn.net");
    // connection.setRequestProperty("Connection", "Keep-Alive");
    // connection.setRequestProperty("Accept-Encoding", "gzip");

     InputStream is = connection.getInputStream();
    
     String string = new String(is2bytes(is), "utf8");
     System.out.println(string);
    

    }

    public static byte[] is2bytes(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];

     ByteOutputStream bos = new ByteOutputStream();
     @SuppressWarnings("unused")
     int len = 0;
     while ((len = is.read(buffer)) != -1) {
     	bos.write(buffer, 0, len);
     }
     bos.flush();
    
     return bos.getBytes();
    

    }
    }

 


数据格式化后的效果图:

https://img.alicdn.com/imgextra/i1/1830641203/TB2wxNNX1zz11Bjy1XdXXbfqVXa_!!1830641203.jpg

 

 

Q.E.D.