Search in sources :

Example 6 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method doPost.

public String doPost(String urlAddress, Map<String, String> param) throws WeiboException {
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
        URL url = new URL(urlAddress);
        Proxy proxy = getProxy();
        HttpsURLConnection uRLConnection;
        if (proxy != null) {
            uRLConnection = (HttpsURLConnection) url.openConnection(proxy);
        } else {
            uRLConnection = (HttpsURLConnection) url.openConnection();
        }
        uRLConnection.setDoInput(true);
        uRLConnection.setDoOutput(true);
        uRLConnection.setRequestMethod("POST");
        uRLConnection.setUseCaches(false);
        uRLConnection.setConnectTimeout(CONNECT_TIMEOUT);
        uRLConnection.setReadTimeout(READ_TIMEOUT);
        uRLConnection.setInstanceFollowRedirects(false);
        uRLConnection.setRequestProperty("Connection", "Keep-Alive");
        uRLConnection.setRequestProperty("Charset", "UTF-8");
        uRLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        uRLConnection.connect();
        DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());
        out.write(Utility.encodeUrl(param).getBytes());
        out.flush();
        out.close();
        return handleResponse(uRLConnection);
    } catch (IOException e) {
        e.printStackTrace();
        throw new WeiboException(errorStr, e);
    }
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) Proxy(java.net.Proxy) WeiboException(org.qii.weiciyuan.support.error.WeiboException) DataOutputStream(java.io.DataOutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

IOException (java.io.IOException)6 InterruptedIOException (java.io.InterruptedIOException)6 WeiboException (org.qii.weiciyuan.support.error.WeiboException)6 GlobalContext (org.qii.weiciyuan.support.utils.GlobalContext)6 FileInputStream (java.io.FileInputStream)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 Proxy (java.net.Proxy)3 URL (java.net.URL)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HttpURLConnection (java.net.HttpURLConnection)2 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1