Java HTTP Request sendGetRequest(String url, String cookies)

Here you can find the source of sendGetRequest(String url, String cookies)

Description

send Get Request

License

Open Source License

Declaration

public static String sendGetRequest(String url, String cookies) throws MalformedURLException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static String sendGetRequest(String url, String cookies) throws MalformedURLException, IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("Cookie", cookies);
        conn.setRequestProperty("Referer", "http://login.sina.com.cn/signup/signin.php?entry=sso");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        BufferedReader read = new BufferedReader(new InputStreamReader(conn.getInputStream(), "gbk"));
        String line = null;/*w  w  w .j  a  va  2s  . c  o  m*/
        StringBuilder ret = new StringBuilder();
        while ((line = read.readLine()) != null) {
            ret.append(line).append("\n");
        }
        StringBuilder ck = new StringBuilder();
        try {
            for (String s : conn.getHeaderFields().get("Set-Cookie")) {
                ck.append(s.split(";")[0]).append(";");
            }

        } catch (Exception e) {
        }
        return ck.toString();
    }
}

Related

  1. requestDataFromUrl(URL url, byte[] tosend, String userAgent)
  2. saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
  3. sendGet(String url)
  4. sendGet(String url, String param)
  5. sendGetRequest(String requestURL)