/** * Specifies the Content-Encoding header, as a string. The default implementation calls {@link * #setContentEncoding(Header) setContentEncoding(Header)}. * * @param ceString the new Content-Encoding header, or <code>null</code> to unset */ public void setContentEncoding(final String ceString) { Header h = null; if (ceString != null) { h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString); } setContentEncoding(h); }
@SuppressWarnings("unused") private void postRemoteRes(String url, final String data) { try { // 设置HttpClient超时参数 HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); // 创建http // client HttpPost httpPost = new HttpPost(url); // 创建http post AbstractHttpEntity entity = null; ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { // outstream.write(data.getBytes(HTTP.UTF_8)); // outstream.flush(); // outstream.close(); Writer writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(data); writer.flush(); writer.close(); } }; entity = new EntityTemplate(cp); entity = new StringEntity(data, "utf-8"); entity = new InputStreamEntity( new ByteArrayInputStream(data.getBytes(HTTP.UTF_8)), data.getBytes(HTTP.UTF_8).length); entity.setContentType("application/x-www-form-urlencoded"); entity.setContentEncoding("utf-8"); httpPost.setEntity(entity); // HttpResponse httpResponse = httpClient.execute(httpPost);// // 执行http 请求 URL urls = new URL(url); HttpURLConnection conn = (HttpURLConnection) urls.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(true); conn.setDoInput(true); conn.connect(); OutputStream out = conn.getOutputStream(); // 默认参数方式传递 out.write(data.getBytes(HTTP.UTF_8)); out.flush(); out.close(); InputStream is = conn.getInputStream(); if (is != null) { // if (httpResponse != null // && httpResponse.getStatusLine().getStatusCode() == // HttpStatus.SC_OK) { // InputStream is = httpResponse.getEntity().getContent(); byte[] buffer = new byte[512]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int readLength = 0; int offset = 0; mDataLen = 0; mDataRes = null; do { readLength = is.read(buffer); if (readLength > 0) { baos.write(buffer, 0, readLength); offset += readLength; mDataLen = offset; } } while (!mIsStopDl && readLength > 0); mDataRes = baos.toByteArray(); baos.close(); is.close(); } // 下载完成 Log.i( TAG, "downloadRemoteRes download completed. data length=" + (mDataRes == null ? "null" : mDataRes.length) + " record length=" + mDataLen + " url=" + url); if (!mIsStopDl && (mDataRes == null || mDataRes.length == 0)) { Log.e(TAG, "data = null"); if (mDlListener != null) mDlListener.onDlError(mTaskTag, DownloadManager.ERROR_RESULT_NULL); return; } if (!mIsStopDl && mDlListener != null) { Log.d(TAG, "downloadRemoteRes ---- callback in."); mDlListener.onDlCompleted(mTaskTag, mDataRes, mDataLen); Log.d(TAG, "downloadRemoteRes ---- callback out."); } } catch (ConnectTimeoutException e) { Log.e(TAG, "downloadRemoteRes exception url=" + url + " msg=" + e.getMessage()); if (!mIsStopDl && mDlListener != null) { mDlListener.onDlError(mTaskTag, DownloadManager.ERROR_TIME_OUT); } } catch (SocketTimeoutException e) { Log.e(TAG, "downloadRemoteRes exception url=" + url + " msg=" + e.getMessage()); if (!mIsStopDl && mDlListener != null) { mDlListener.onDlError(mTaskTag, DownloadManager.ERROR_TIME_OUT); } } catch (Exception e) { Log.e(TAG, "downloadRemoteRes exception url=" + url + " msg=" + e.getMessage()); if (!mIsStopDl && mDlListener != null) { mDlListener.onDlError(mTaskTag, DownloadManager.ERROR_OTHER); } } }