cz.msebera.android.httpclient.protocol.HTTP Java Examples

The following examples show how to use cz.msebera.android.httpclient.protocol.HTTP. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InstaUtils.java    From Instagram-Profile-Downloader with MIT License 6 votes vote down vote up
public static String getInitCookies(String url) throws Exception {
    URLConnection urlConn = new URL(url).openConnection();
    if (urlConn instanceof HttpURLConnection) {
        HttpURLConnection httpConn = (HttpURLConnection) urlConn;
        httpConn.setRequestMethod(HttpGet.METHOD_NAME);
        httpConn.setRequestProperty(HTTP.USER_AGENT, USER_AGENT);
        httpConn.setRequestProperty(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpConn.setRequestProperty(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.5");
        httpConn.connect();
        int resCode = httpConn.getResponseCode();
        BufferedReader rd = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
        StringBuffer result = new StringBuffer();
        String str;
        while (true) {
            str = rd.readLine();
            if (str != null) {
                result.append(str);
            } else {
                extractAndSetCookies(httpConn);
                return result.toString();
            }
        }
    }
    throw new IOException("URL is not an Http URL");
}
 
Example #2
Source File: InstaUtils.java    From Instagram-Profile-Downloader with MIT License 6 votes vote down vote up
public static HttpURLConnection addPostHeaders(HttpURLConnection post) {
    post.setRequestProperty("Origin", "https://www.instagram.com");
    post.setRequestProperty("accept-encoding", "gzip, deflate, br");
    post.setRequestProperty(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.5");
    post.setRequestProperty(HTTP.USER_AGENT, USER_AGENT);
    post.setRequestProperty("x-requested-with", "XMLHttpRequest");
    post.setRequestProperty("x-csrftoken", getCsrf());
    post.setRequestProperty("x-instagram-ajax", "1");
    post.setRequestProperty(HTTP.CONTENT_TYPE, URLEncodedUtils.CONTENT_TYPE);
    post.setRequestProperty(HttpHeaders.ACCEPT, "*/*");
    post.setRequestProperty(HttpHeaders.REFERER, "https://www.instagram.com/");
    post.setRequestProperty(SM.COOKIE, getCookies());
    post.setRequestProperty("authority", "https://www.instagram.com/");
    post.setRequestProperty(HTTP.CONN_DIRECTIVE, "keep-alive");
    return post;
}
 
Example #3
Source File: InstaUtils.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
public static String login(String username, String pass) throws Exception {
    getInitCookies(ig);

    URLConnection connection = new URL(igAuth).openConnection();
    if(connection instanceof HttpURLConnection){
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setRequestMethod(HttpPost.METHOD_NAME);
        httpURLConnection = addPostHeaders(httpURLConnection);

        String query = new Builder().appendQueryParameter("username", username).appendQueryParameter("password", pass).build().getEncodedQuery();
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, HTTP.UTF_8));
        bufferedWriter.write(query);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        httpURLConnection.connect();

        if(httpURLConnection.getResponseCode() != HttpStatus.SC_OK){
            return "bad_request";
        }

        extractAndSetCookies(httpURLConnection);
        JSONObject jsonObject = new JSONObject(buildResultString(httpURLConnection));
        if(jsonObject.get("user").toString().isEmpty()){
            return BuildConfig.VERSION_NAME;
        }

        return jsonObject.get("authenticated").toString();
    }

    throw new IOException("Url is not valid");
}
 
Example #4
Source File: InstaUtils.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
public static ArrayList<String> stories(String userId, Context context) {
    try {
        HttpURLConnection httpConn = (HttpURLConnection) new URL("https://i.instagram.com/api/v1/feed/user/" + userId + "/reel_media/").openConnection();
        httpConn.setRequestMethod(HttpGet.METHOD_NAME);
        httpConn.setRequestProperty("accept-encoding", "gzip, deflate, br");
        httpConn.setRequestProperty("x-ig-capabilities", "3w==");
        httpConn.setRequestProperty(HttpHeaders.ACCEPT_LANGUAGE, "en-GB,en-US;q=0.8,en;q=0.6");
        httpConn.setRequestProperty(HTTP.USER_AGENT, "Instagram 9.5.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/420+");
        httpConn.setRequestProperty(HttpHeaders.ACCEPT, "*/*");
        httpConn.setRequestProperty(HttpHeaders.REFERER, "https://www.instagram.com/");
        httpConn.setRequestProperty("authority", "i.instagram.com/");
        if (getUserId() == null) {
            setUserId(ZoomstaUtil.getStringPreference(context, "userid"));
        }
        if (getSessionid() == null) {
            setSessionId(ZoomstaUtil.getStringPreference(context, "sessionid"));
        }
        httpConn.setRequestProperty(SM.COOKIE, "ds_user_id=" + getUserId() + "; sessionid=" + getSessionid() + ";");
        int responseCode = httpConn.getResponseCode();
        String result = buildResultString(httpConn);
        List<String> imageList = new ArrayList();
        List<String> videoList = new ArrayList();
        List<String> videoThumbs = new ArrayList();
        try {
            vids.clear();
            JSONArray array = new JSONObject(result).getJSONArray("items");
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObj = array.getJSONObject(i);
                JSONArray video = itemObj.optJSONArray("video_versions");
                if (video != null) {
                    videoList.add(video.getJSONObject(0).getString("url"));
                    JSONArray imageArray = itemObj.getJSONObject("image_versions2").getJSONArray("candidates");
                    videoThumbs.add(imageArray.getJSONObject(imageArray.length() - 1).getString("url"));
                    vids.put(imageArray.getJSONObject(imageArray.length() - 1).getString("url"), video.getJSONObject(0).getString("url"));
                    Log.d("videos" , "" + video.getJSONObject(0).getString("url"));
                } else {
                    String url = itemObj.getJSONObject("image_versions2").getJSONArray("candidates").getJSONObject(0).getString("url");
                    if (!url.endsWith(".jpg")) {
                        url = url + ".jpg";
                    }
                    imageList.add(url);
                }
            }
            stories.clear();
            stories.addAll(imageList);
            stories.addAll(videoThumbs);

            /*for(int i=0; i<stories.size(); i++){
                String model = stories.get(i);
                Log.d("stories", "" + model);
            }*/
        } catch (Exception e) {
            System.out.println(e);
        }
    } catch (Exception e2) {
        System.out.println(e2);
    }
    return stories;
}
 
Example #5
Source File: InstaUtils.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
public static ArrayList<StoryModel> fetchStories(String userId, Context context) {
    try {
        HttpURLConnection httpConn = (HttpURLConnection) new URL("https://i.instagram.com/api/v1/feed/user/" + userId + "/reel_media/").openConnection();
        httpConn.setRequestMethod(HttpGet.METHOD_NAME);
        httpConn.setRequestProperty("accept-encoding", "gzip, deflate, br");
        httpConn.setRequestProperty("x-ig-capabilities", "3w==");
        httpConn.setRequestProperty(HttpHeaders.ACCEPT_LANGUAGE, "en-GB,en-US;q=0.8,en;q=0.6");
        httpConn.setRequestProperty(HTTP.USER_AGENT, "Instagram 9.5.2 (iPhone7,2; iPhone OS 9_3_3; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/420+");
        httpConn.setRequestProperty(HttpHeaders.ACCEPT, "*/*");
        httpConn.setRequestProperty(HttpHeaders.REFERER, "https://www.instagram.com/");
        httpConn.setRequestProperty("authority", "i.instagram.com/");
        if (getUserId() == null) {
            setUserId(ZoomstaUtil.getStringPreference(context, "userid"));
        }
        if (getSessionid() == null) {
            setSessionId(ZoomstaUtil.getStringPreference(context, "sessionid"));
        }
        httpConn.setRequestProperty(SM.COOKIE, "ds_user_id=" + getUserId() + "; sessionid=" + getSessionid() + ";");
        int responseCode = httpConn.getResponseCode();
        String result = buildResultString(httpConn);
        List<StoryModel> imageList = new ArrayList();
        List<StoryModel> videoList = new ArrayList();
        List<StoryModel> videoThumbs = new ArrayList();
        try {
            vids.clear();
            JSONArray array = new JSONObject(result).getJSONArray("items");
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObj = array.getJSONObject(i);
                JSONArray video = itemObj.optJSONArray("video_versions");
                if (video != null) {
                    StoryModel model = new StoryModel();
                    model.setFilePath(video.getJSONObject(0).getString("url"));
                    //Log.d("addvid", "" + video.getJSONObject(0).getString("url") );
                    model.setFileName(null);
                    model.setType(1);
                    model.setSaved(false);
                    videoList.add(model);

                    JSONArray imageArray = itemObj.getJSONObject("image_versions2").getJSONArray("candidates");
                    StoryModel storyModel = new StoryModel();
                    model.setFilePath(imageArray.getJSONObject(imageArray.length() - 1).getString("url"));
                    storyModel.setFileName(null);
                    storyModel.setType(1);
                    storyModel.setSaved(false);
                    videoThumbs.add(storyModel);
                    vids.put(imageArray.getJSONObject(imageArray.length() - 1).getString("url"), video.getJSONObject(0).getString("url"));
                } else {
                    String url = itemObj.getJSONObject("image_versions2").getJSONArray("candidates").getJSONObject(0).getString("url");
                    if (!url.endsWith(".jpg")) {
                        url = url.split(".jpg")[0] + ".jpg";
                    }
                    StoryModel imageStories = new StoryModel();
                    imageStories.setFilePath(url);
                    imageStories.setFileName(null);
                    imageStories.setSaved(false);
                    imageStories.setType(0);
                    imageList.add(imageStories);
                }
            }
            getStories.clear();
            getStories.addAll(imageList);
            getStories.addAll(videoList);

        } catch (Exception e) {
            System.out.println(e);
        }
    } catch (Exception e2) {
        System.out.println(e2);
    }
    return getStories;
}