From ce786f4e416c1710f4f20832e4cbaf52ae39d62f Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 21 Jun 2014 18:20:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=20HttpHelper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hippo/ehviewer/AppContext.java | 1 - src/com/hippo/ehviewer/ehclient/EhClient.java | 12 +- src/com/hippo/ehviewer/ehclient/EhInfo.java | 192 +++++++ .../hippo/ehviewer/ehclient/ListParser.java | 6 +- .../hippo/ehviewer/network/HttpHelper.java | 503 ++++++------------ 5 files changed, 365 insertions(+), 349 deletions(-) create mode 100644 src/com/hippo/ehviewer/ehclient/EhInfo.java diff --git a/src/com/hippo/ehviewer/AppContext.java b/src/com/hippo/ehviewer/AppContext.java index 3508034d4..45afb4353 100644 --- a/src/com/hippo/ehviewer/AppContext.java +++ b/src/com/hippo/ehviewer/AppContext.java @@ -65,7 +65,6 @@ public class AppContext extends Application implements UncaughtExceptionHandler Favorite.init(this); mData = new Data(this); - HttpHelper.setCookieHelper(this); mImageGeterManager = new ImageGeterManager(this, Cache.memoryCache, Cache.diskCache); LoadImageView.setImageGeterManager(mImageGeterManager); diff --git a/src/com/hippo/ehviewer/ehclient/EhClient.java b/src/com/hippo/ehviewer/ehclient/EhClient.java index f02121959..167d0f5ed 100644 --- a/src/com/hippo/ehviewer/ehclient/EhClient.java +++ b/src/com/hippo/ehviewer/ehclient/EhClient.java @@ -371,7 +371,7 @@ public class EhClient { new String[] { "CookieDate", "1" }, new String[] { "temporary_https", "on" }}; HttpHelper hp = new HttpHelper(mAppContext); - String pageContent = hp.post(loginUrl, args); + String pageContent = hp.postForm(loginUrl, args); if (pageContent != null) { if (pageContent.contains("

You are now logged in as: ")) ok = true; @@ -473,7 +473,7 @@ public class EhClient { public void onSuccess(); public void onFailure(String eMsg); } - + /* public void testEx(final OnTestExListener listener) { new Thread(new Runnable() { @Override @@ -496,7 +496,7 @@ public class EhClient { } }).start(); } - + */ // Logout private class LogoutPackage { public boolean ok; @@ -1483,7 +1483,7 @@ public class EhClient { listener.onFailure(eMsg); // TODO } }); - hp.post(detailUrl, new String[][]{ + hp.postForm(detailUrl, new String[][]{ new String[]{"commenttext", comment}, new String[]{"postcomment", "Post New"}}); } @@ -1654,7 +1654,7 @@ public class EhClient { catStr = "0"; // submit=Add+to+Favorites is not necessary, just use submit=Apply+Changes all the time - hp.post(getAddFavouriteUrl(gid, token), new String[][]{ + hp.postForm(getAddFavouriteUrl(gid, token), new String[][]{ new String[]{"favcat", catStr}, new String[]{"favnote", note == null ? "" : note}, new String[]{"submit", "Apply Changes"}}); @@ -1779,7 +1779,7 @@ public class EhClient { args[i] = new String[]{"modifygids[]", String.valueOf(gids[i-1])}; args[i] = new String[]{"apply", "Apply"}; - hp.post(getFavoriteUrl(srcCat, 0), args); + hp.postForm(getFavoriteUrl(srcCat, 0), args); } }).start(); } diff --git a/src/com/hippo/ehviewer/ehclient/EhInfo.java b/src/com/hippo/ehviewer/ehclient/EhInfo.java new file mode 100644 index 000000000..18a6b2951 --- /dev/null +++ b/src/com/hippo/ehviewer/ehclient/EhInfo.java @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2014 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.ehclient; + +import java.net.HttpURLConnection; +import java.util.List; + +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; + +/** + * + * @author Hippo + * + */ +public class EhInfo { + + private static final String TAG = EhInfo.class.getSimpleName(); + private static final String PREF_NAME = "eh_info"; + + public static final String[] COOKIABLE_HOSTS = {"exhentai.org", + "g.e-hentai.org", "forums.e-hentai.org"}; + + private static final String KEY_DEFAULT_CAT = "default_cat"; + private static final int DEFAULT_DEFAULT_CAT = 0; + + // m for normal, l for large + private static final String KEY_PREVIEW_MODE = "preview_mode"; + private static final String DEFAULT_PREVIEW_MODE = "m"; + + /** + * reclass 0x1 + * language 0x2 + * parody 0x4 + * character 0x8 + * group 0x10 + * artist 0x20 + * male 0x40 + * female 0x80 + */ + private static final String KEY_EXCLUDE_TAG_GROUP = "exculde_tag_group"; + private static final int DEFAULT_EXCLUDE_TAG_GROUP = 0; + + /** + * Original Translated Rewrite All + * Japanese 1024 2048 + * English 1 1025 2049 + * Chinese 10 1034 2058 + * Dutch 20 1044 2068 + * French 30 1054 2078 + * German 40 1064 2088 + * Hungarian 50 1074 2098 + * Italian 60 1084 2108 + * Korean 70 1094 2118 + * Polish 80 1104 2128 + * Portuguese 90 1114 2138 + * Russian 100 1124 2148 + * Spanish 110 1134 2158 + * Thai 120 1144 2168 + * Vietnamese 130 1154 2178 + * Other 255 1279 2303 + */ + private static final String KEY_EXCLUDE_LANGUAGE = "exculde_tag_group"; + private static final String DEFAULT_EXCLUDE_LANGUAGE = ""; + + private static final String KEY_LOGIN = "login"; + private static final boolean DEFAULT_LOGIN = false; + private static final String KEY_USERNAME = "username"; + private static final String DEFAULT_USERNAME = "Hippo"; + + + private Context mContext; + private final SharedPreferences mInfoPref; + private String mUconfig; + + private static EhInfo sInstance; + + private EhInfo(final Context context){ + mContext = context; + mInfoPref = mContext.getSharedPreferences(PREF_NAME, 0); + + updateUconfig(); + }; + + private void updateUconfig() { + mUconfig = "cats_" + mInfoPref.getInt(KEY_DEFAULT_CAT, DEFAULT_DEFAULT_CAT) + + "-ts_" + mInfoPref.getString(KEY_PREVIEW_MODE, DEFAULT_PREVIEW_MODE) + + "-xns_" + mInfoPref.getInt(KEY_EXCLUDE_TAG_GROUP, DEFAULT_EXCLUDE_TAG_GROUP) + + "-xl_" + mInfoPref.getString(KEY_EXCLUDE_LANGUAGE, DEFAULT_EXCLUDE_LANGUAGE) + + "-tl_m-uh_y-tr_2-prn_n-dm_l-ar_0-rc_0-rx_0-ry_0-sa_y-oi_n-qb_n-tf_n-hp_-hk_-ms_n-mt_n"; + } + + public final static EhInfo getInstance(final Context context) { + if (sInstance == null) + sInstance = new EhInfo(context.getApplicationContext()); + return sInstance; + } + + private String getCookie(String cookieStr, String key) { + String value = null; + int index1 = -1; + int index2 = -1; + + if ((index1 = cookieStr.indexOf(key + "=")) != -1) { + index1 += key.length() + 1; // size of key + "=" + if ((index2 = cookieStr.indexOf(";", index1)) != -1) + value = cookieStr.substring(index1, index2); + else + value = cookieStr.substring(index1); + } + return value; + } + + public void storeCookie(HttpURLConnection conn) { + String ipb_member_id; + String ipb_pass_hash; + SharedPreferences.Editor editor = mInfoPref.edit(); + + List cookieList = conn.getHeaderFields().get("Set-Cookie"); + for (String str : cookieList) { + ipb_member_id = getCookie(str, "ipb_member_id"); + ipb_pass_hash = getCookie(str, "ipb_pass_hash"); + + if (ipb_member_id != null) + editor.putString("ipb_member_id", ipb_member_id); + if (ipb_pass_hash != null) + editor.putString("ipb_pass_hash", ipb_pass_hash); + } + editor.apply(); + } + + public void setCookie(HttpURLConnection conn) { + String cookie = "ipb_member_id=" + mInfoPref.getString("ipb_member_id", "ipb_member_id") + + "; ipb_pass_hash=" + mInfoPref.getString("ipb_pass_hash", "ipb_pass_hash") + + "; uconfig="+ mUconfig; + conn.setRequestProperty("Cookie", cookie); + } + + public boolean isLogin() { + return mInfoPref.getBoolean(KEY_LOGIN, DEFAULT_LOGIN); + } + + public void login(String username) { + mInfoPref.edit().putBoolean(KEY_LOGIN, true) + .putString(KEY_USERNAME, username).apply(); + } + + public void logout() { + mInfoPref.edit().putBoolean(KEY_LOGIN, false) + .putString("ipb_member_id", "ipb_member_id") + .putString("ipb_pass_hash", "ipb_pass_hash").apply(); + } + + public String getUsername() { + return mInfoPref.getString(KEY_USERNAME, DEFAULT_USERNAME); + } + + public void setDefaultCat(int defaultCat) { + mInfoPref.edit().putInt(KEY_DEFAULT_CAT, defaultCat).commit(); + updateUconfig(); + } + + public void setPreviewMode(String previewMode) { + mInfoPref.edit().putString(KEY_PREVIEW_MODE, previewMode).commit(); + updateUconfig(); + } + + public void setExcludeTagGroup(int excludeTagGroup) { + mInfoPref.edit().putInt(KEY_EXCLUDE_TAG_GROUP, excludeTagGroup).commit(); + updateUconfig(); + } + + public void setExcludeLanguage(String excludeLanguage) { + mInfoPref.edit().putString(KEY_EXCLUDE_LANGUAGE, excludeLanguage).commit(); + updateUconfig(); + } +} diff --git a/src/com/hippo/ehviewer/ehclient/ListParser.java b/src/com/hippo/ehviewer/ehclient/ListParser.java index efd4595e7..6ffacf75b 100644 --- a/src/com/hippo/ehviewer/ehclient/ListParser.java +++ b/src/com/hippo/ehviewer/ehclient/ListParser.java @@ -74,12 +74,10 @@ public class ListParser { if (m.group(3) == null) { gi.thumb = "http://" + m.group(5).replace('~', '/'); - gi.title = StringEscapeUtils.unescapeHtml4(m - .group(6)); + gi.title = m.group(6); } else { gi.thumb = m.group(3); - gi.title = StringEscapeUtils.unescapeHtml4(m - .group(4)); + gi.title = m.group(4); } Pattern pattern = Pattern diff --git a/src/com/hippo/ehviewer/network/HttpHelper.java b/src/com/hippo/ehviewer/network/HttpHelper.java index 837117fe0..715f7f84b 100644 --- a/src/com/hippo/ehviewer/network/HttpHelper.java +++ b/src/com/hippo/ehviewer/network/HttpHelper.java @@ -26,6 +26,7 @@ import java.net.CookieManager; import java.net.CookiePolicy; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.ProtocolException; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URL; @@ -40,6 +41,8 @@ import org.json.JSONObject; import com.hippo.ehviewer.DiskCache; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.ehclient.EhClient; +import com.hippo.ehviewer.ehclient.EhInfo; import com.hippo.ehviewer.exception.RedirectionException; import com.hippo.ehviewer.exception.ResponseCodeException; import com.hippo.ehviewer.exception.SadPandaException; @@ -65,7 +68,7 @@ import com.hippo.ehviewer.util.Log; public class HttpHelper { private static final String TAG = "HttpHelper"; public static final String SAD_PANDA_ERROR = "Sad Panda"; - + public static final String HAPPY_PANDA_BODY = "Happy Panda"; private static String DEFAULT_CHARSET = "utf-8"; private static String CHARSET_KEY = "charset="; @@ -109,11 +112,6 @@ public class HttpHelper { mContext = context; } - public static void setCookieHelper(Context context) { - CookieManager cookieManager = new CookieManager(new ShapreCookieStore(context), CookiePolicy.ACCEPT_ALL); - CookieHandler.setDefault(cookieManager); - } - /** * Get last error message * @return @@ -192,20 +190,28 @@ public class HttpHelper { return pageContext; } - public String get(String urlStr) { + private boolean isUrlCookiable(URL url) { + String host = url.getHost(); + for (String h : EhInfo.COOKIABLE_HOSTS) { + if (h.equals(host)) + return true; + } + return false; + } + + private String requst(RequestHelper rh, String urlStr) { mException = null; - int redirectionCount = 0; URL url = null; HttpURLConnection conn = null; Message msg = null; + if (mListener != null) msg = new Message(); try { - - Log.d(TAG, "Http get " + urlStr); - + Log.d(TAG, "Requst " + urlStr); url = new URL(urlStr); + boolean isCookiable = isUrlCookiable(url); while (redirectionCount++ < Constant.MAX_REDIRECTS) { conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(true); @@ -213,9 +219,13 @@ public class HttpHelper { conn.setRequestProperty("User-Agent", Constant.userAgent); conn.setConnectTimeout(Constant.DEFAULT_TIMEOUT); conn.setReadTimeout(Constant.DEFAULT_TIMEOUT); - conn.setRequestProperty("Connection", "close"); - conn.connect(); + // Set cookie if necessary + if (isCookiable) + EhInfo.getInstance(mContext).setCookie(conn); + // Do custom staff + rh.onBeforeConnect(conn); + conn.connect(); final int responseCode = conn.getResponseCode(); switch (responseCode) { case HttpURLConnection.HTTP_OK: @@ -224,18 +234,24 @@ public class HttpHelper { String contentType = conn.getHeaderField("Content-Type"); if (contentType != null && contentType.equals("image/gif")) throw new SadPandaException(); - - String pageContext = getPageContext(conn); + // Store cookie if necessary + if (isCookiable) + EhInfo.getInstance(mContext).storeCookie(conn); + // Get body if necessary + String body = rh.isNeedBody(); + if (body == null) + body = getPageContext(conn); + // Send to UI thread if necessary if (msg != null) { Package p = new Package(); p.listener = mListener; - p.str = pageContext; + p.str = body; msg.obj = p; msg.what = Constants.TRUE; mHandler.sendMessage(msg); } - return pageContext; - + return body; + // redirect case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: @@ -248,7 +264,7 @@ public class HttpHelper { throw new ResponseCodeException(responseCode); } } - } catch (Exception e) { + } catch (Exception e) { mException = e; e.printStackTrace(); @@ -265,346 +281,157 @@ public class HttpHelper { if (conn != null) conn.disconnect(); } - mException = new RedirectionException(); return null; } - public String contentType(String urlStr) { - mException = null; - - int redirectionCount = 0; - URL url = null; - HttpURLConnection conn = null; - Message msg = null; - if (mListener != null) - msg = new Message(); - try { - - Log.d(TAG, "Http head " + urlStr); - - url = new URL(urlStr); - while (redirectionCount++ < Constant.MAX_REDIRECTS) { - conn = (HttpURLConnection) url.openConnection(); - conn.setInstanceFollowRedirects(true); - conn.connect(); - - String contentType = conn.getHeaderField("Content-Type"); - contentType = contentType == null ? "" : contentType; - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = contentType; - msg.obj = p; - msg.what = Constants.TRUE; - mHandler.sendMessage(msg); - } - return contentType; - } - } catch (Exception e) { - mException = e; - e.printStackTrace(); - - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = getEMsg(); - msg.obj = p; - msg.what = Constants.FALSE; - mHandler.sendMessage(msg); - } - return null; - } finally { - if (conn != null) - conn.disconnect(); + interface RequestHelper { + public void onBeforeConnect(HttpURLConnection conn) throws Exception; + /** + * If need body, return null, otherwise return a string used as body + * @return + */ + public String isNeedBody(); + } + + /** + * RequstHelper for check sad panda, use HEAD method + */ + private class CheckSpHelper implements RequestHelper { + @Override + public void onBeforeConnect(HttpURLConnection conn) + throws ProtocolException { + conn.setRequestMethod("HEAD"); } - mException = new RedirectionException(); - return null; + @Override + public String isNeedBody() { + return HAPPY_PANDA_BODY; + } } - public String post(String urlStr, String[][] args) { - mException = null; - - int redirectionCount = 0; - URL url = null; - HttpURLConnection conn = null; - - Message msg = null; - if (mListener != null) - msg = new Message(); - - try { - - Log.d(TAG, "Http post " + urlStr); - - url = new URL(urlStr); - while (redirectionCount++ < Constant.MAX_REDIRECTS) { - conn = (HttpURLConnection) url.openConnection(); - conn.addRequestProperty("Accept-Encoding", "gzip"); - conn.setRequestProperty("User-Agent", Constant.userAgent); - conn.setConnectTimeout(Constant.DEFAULT_TIMEOUT); - conn.setReadTimeout(Constant.DEFAULT_TIMEOUT); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod("POST"); - conn.setUseCaches(false); - conn.setInstanceFollowRedirects(true); - conn.setRequestProperty("Content-Type", - "application/x-www-form-urlencoded"); - conn.setRequestProperty("Connection", "close"); - - DataOutputStream out = new DataOutputStream(conn.getOutputStream()); - StringBuilder sb = new StringBuilder(); - int i = 0; - for (String[] arg : args) { - if (i != 0) - sb.append("&"); - sb.append(URLEncoder.encode(arg[0], "UTF-8")); - sb.append("="); - sb.append(URLEncoder.encode(arg[1], "UTF-8")); - i++; - } - Log.d(TAG, sb.toString()); - out.writeBytes(sb.toString()); - out.flush(); - out.close(); - - conn.connect(); - - final int responseCode = conn.getResponseCode(); - switch (responseCode) { - case HttpURLConnection.HTTP_OK: - case HttpURLConnection.HTTP_PARTIAL: - // Test sad panda - String contentType = conn.getHeaderField("Content-Type"); - if (contentType != null && contentType.equals("image/gif")) - throw new SadPandaException(); - - String pageContext = getPageContext(conn); - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = pageContext; - msg.obj = p; - msg.what = Constants.TRUE; - mHandler.sendMessage(msg); - } - return pageContext; - - case HttpURLConnection.HTTP_MOVED_PERM: - case HttpURLConnection.HTTP_MOVED_TEMP: - case HttpURLConnection.HTTP_SEE_OTHER: - case Constant.HTTP_TEMP_REDIRECT: - final String location = conn.getHeaderField("Location"); - url = new URL(url, location); - continue; - - default: - throw new ResponseCodeException(responseCode); - } - } - } catch (Exception e) { - mException = e; - e.printStackTrace(); - - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = getEMsg(); - msg.obj = p; - msg.what = Constants.FALSE; - mHandler.sendMessage(msg); - } + /** + * RequstHelper for GET method + */ + private class GetHelper implements RequestHelper { + @Override + public void onBeforeConnect(HttpURLConnection conn) + throws ProtocolException { + conn.setRequestMethod("GET"); + } + @Override + public String isNeedBody() { return null; - } finally { - if (conn != null) - conn.disconnect(); + } + } + + /** + * RequstHelper for post form data, use POST method + */ + private class PostFormHelper implements RequestHelper { + private String[][] mArgs; + + public PostFormHelper(String[][] args) { + mArgs = args; } - mException = new RedirectionException(); - return null; + @Override + public void onBeforeConnect(HttpURLConnection conn) + throws Exception { + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setUseCaches(false); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", + "application/x-www-form-urlencoded"); + + DataOutputStream out = new DataOutputStream(conn.getOutputStream()); + StringBuilder sb = new StringBuilder(); + int i = 0; + for (String[] arg : mArgs) { + if (i != 0) + sb.append("&"); + sb.append(URLEncoder.encode(arg[0], "UTF-8")); + sb.append("="); + sb.append(URLEncoder.encode(arg[1], "UTF-8")); + i++; + } + Log.d(TAG, sb.toString()); + out.writeBytes(sb.toString()); + out.flush(); + out.close(); + } + @Override + public String isNeedBody() { + return null; + } } - /* - public String post(String urlStr, String str) { - mException = null; + /** + * RequstHelper for post json, use POST method + */ + private class PostJsonHelper implements RequestHelper { + private JSONObject mJo; - int redirectionCount = 0; - URL url = null; - HttpURLConnection conn = null; - - Message msg = null; - if (mListener != null) - msg = new Message(); - - try { - - Log.d(TAG, "Http post " + urlStr); - - url = new URL(urlStr); - while (redirectionCount++ < Constant.MAX_REDIRECTS) { - conn = (HttpURLConnection) url.openConnection(); - conn.addRequestProperty("Accept-Encoding", "gzip"); - conn.setRequestProperty("User-Agent", Constant.userAgent); - conn.setConnectTimeout(Constant.DEFAULT_TIMEOUT); - conn.setReadTimeout(Constant.DEFAULT_TIMEOUT); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod("POST"); - conn.setUseCaches(false); - conn.setInstanceFollowRedirects(true); - conn.setRequestProperty("Connection", "close"); - - DataOutputStream out = new DataOutputStream(conn.getOutputStream()); - out.writeBytes(str); - out.flush(); - out.close(); - - conn.connect(); - - final int responseCode = conn.getResponseCode(); - switch (responseCode) { - case HttpURLConnection.HTTP_OK: - case HttpURLConnection.HTTP_PARTIAL: - String pageContext = getPageContext(conn); - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = pageContext; - msg.obj = p; - msg.what = Constants.TRUE; - mHandler.sendMessage(msg); - } - return pageContext; - - case HttpURLConnection.HTTP_MOVED_PERM: - case HttpURLConnection.HTTP_MOVED_TEMP: - case HttpURLConnection.HTTP_SEE_OTHER: - case Constant.HTTP_TEMP_REDIRECT: - final String location = conn.getHeaderField("Location"); - url = new URL(url, location); - continue; - - default: - throw new ResponseCodeException(responseCode); - } - } - } catch (Exception e) { - mException = e; - e.printStackTrace(); - - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = getEMsg(); - msg.obj = p; - msg.what = Constants.FALSE; - mHandler.sendMessage(msg); - } - return null; - } finally { - if (conn != null) - conn.disconnect(); + public PostJsonHelper(JSONObject jo) { + mJo = jo; } - mException = new RedirectionException(); - return null; - } - */ - - public String postJson(String urlStr, JSONObject json) { - mException = null; - - int redirectionCount = 0; - URL url = null; - HttpURLConnection conn = null; - - Message msg = null; - if (mListener != null) - msg = new Message(); - - try { + @Override + public void onBeforeConnect(HttpURLConnection conn) throws Exception { + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setUseCaches(false); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json"); - Log.d(TAG, "Http post " + urlStr); - - url = new URL(urlStr); - while (redirectionCount++ < Constant.MAX_REDIRECTS) { - conn = (HttpURLConnection) url.openConnection(); - conn.addRequestProperty("Accept-Encoding", "gzip"); - conn.setRequestProperty("User-Agent", Constant.userAgent); - conn.setConnectTimeout(Constant.DEFAULT_TIMEOUT); - conn.setReadTimeout(Constant.DEFAULT_TIMEOUT); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod("POST"); - conn.setUseCaches(false); - conn.setInstanceFollowRedirects(true); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Accept", "application/json"); - conn.setRequestProperty("Connection", "close"); - - DataOutputStream out = new DataOutputStream(conn.getOutputStream()); - out.writeBytes(json.toString()); - out.flush(); - out.close(); - - conn.connect(); - - final int responseCode = conn.getResponseCode(); - switch (responseCode) { - case HttpURLConnection.HTTP_OK: - case HttpURLConnection.HTTP_PARTIAL: - // Test sad panda - String contentType = conn.getHeaderField("Content-Type"); - if (contentType != null && contentType.equals("image/gif")) - throw new SadPandaException(); - - String pageContext = getPageContext(conn); - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = pageContext; - msg.obj = p; - msg.what = Constants.TRUE; - mHandler.sendMessage(msg); - } - return pageContext; - - case HttpURLConnection.HTTP_MOVED_PERM: - case HttpURLConnection.HTTP_MOVED_TEMP: - case HttpURLConnection.HTTP_SEE_OTHER: - case Constant.HTTP_TEMP_REDIRECT: - final String location = conn.getHeaderField("Location"); - url = new URL(url, location); - continue; - - default: - throw new ResponseCodeException(responseCode); - } - } - } catch (Exception e) { - mException = e; - e.printStackTrace(); - - if (msg != null) { - Package p = new Package(); - p.listener = mListener; - p.str = getEMsg(); - msg.obj = p; - msg.what = Constants.FALSE; - mHandler.sendMessage(msg); - } + DataOutputStream out = new DataOutputStream(conn.getOutputStream()); + out.writeBytes(mJo.toString()); + out.flush(); + out.close(); + } + @Override + public String isNeedBody() { return null; - } finally { - if (conn != null) - conn.disconnect(); } - - mException = new RedirectionException(); - return null; } + /** + * Check Sad Panda + * @return + */ + public String checkSadPanda() { + return requst(new CheckSpHelper(), EhClient.EX_HEADER); + } + + /** + * Http GET method + * @param url + * @return + */ + public String get(String url) { + return requst(new GetHelper(), url); + } + + /** + * Post form data + * @param url + * @param args + * @return + */ + public String postForm(String url, String[][] args) { + return requst(new PostFormHelper(args), url); + } + + /** + * Post json data + * @param url + * @param json + * @return + */ + public String postJson(String url, JSONObject json) { + return requst(new PostJsonHelper(json), url); + } public Bitmap getImage(String urlStr, String key, LruCache memoryCache,