Java源码示例:cz.msebera.android.httpclient.client.methods.HttpGet
示例1
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");
}
示例2
public static String getContent(String url) throws Exception {
StringBuilder sb = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpParams httpParams = (HttpParams) client.getParams();
// 设置网络超时参数
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent(), "GBK"), 8192);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
}
return sb.toString();
}
示例3
public static CloseableHttpResponse get(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws IOException {
System.out.println("url:"+url);
System.out.println("params:"+MapUtil.getEncodedUrl(params));
System.out.println("headers:"+MapUtil.getEncodedUrl(headers));
String urlParamString = MapUtil.getEncodedUrl(params);
if(urlParamString.length()>0){
if(url.contains("?")){
url = String.format("%s&%s",url,urlParamString);
}else{
url = String.format("%s?%s",url,urlParamString);
}
}
HttpGet httpGet = new HttpGet(url);
for(Map.Entry<String,String> entry:headers.entrySet()){
httpGet.setHeader(entry.getKey(),entry.getValue());
}
return httpClient.execute(httpGet);
}
示例4
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例5
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例6
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例7
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例8
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(final_url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例9
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(getResources().getString(R.string.get_user_info_url) + token);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例10
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例11
@Override
protected String doInBackground(Void... voids) {
String ret = null;
CloseableHttpClient chc = HttpClients.createDefault();
try {
CloseableHttpResponse chr = chc.execute(HttpHost.create("https://httpbin.org"), new HttpGet("/headers"));
ret = EntityUtils.toString(chr.getEntity());
chr.close();
chc.close();
} catch (Exception e) {
Log.e("HttpTest", e.getMessage(), e);
ret = e.getMessage();
}
return ret;
}
示例12
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;
}
示例13
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;
}
示例14
public static String getResponseFromGetUrl(String url,
String params) throws Exception {
Log.d("Chen", "url--" + url);
if (null != params && !"".equals(params)) {
url = url + "?";
String[] paramarray = params.split(",");
for (int index = 0; null != paramarray && index < paramarray.length; index++) {
if (index == 0) {
url = url + paramarray[index];
} else {
url = url + "&" + paramarray[index];
}
}
}
HttpGet httpRequest = new HttpGet(url);
// httpRequest.addHeader("Cookie", logininfo);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
// DefaultHttpClient httpclient = new DefaultHttpClient();
StringBuffer sb = new StringBuffer();
try {
HttpResponse httpResponse = httpclient.execute(httpRequest);
String inputLine = "";
// Log.d("Chen","httpResponse.getStatusLine().getStatusCode()"+httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStreamReader is = new InputStreamReader(httpResponse
.getEntity().getContent());
BufferedReader in = new BufferedReader(is);
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
} else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
httpclient.getConnectionManager().shutdown();
}
return sb.toString();
}