1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
public byte[] execute(String urlString, byte[] pdu, String method, boolean isProxySet, String proxyHost, int proxyPort, Bundle mmsConfig, int subId, String requestId) throws MmsHttpException { LogUtil.d(requestId, "HTTP: " + method + " " + redactUrlForNonVerbose(urlString) + (isProxySet ? (", proxy=" + proxyHost + ":" + proxyPort) : "") + ", PDU size=" + (pdu != null ? pdu.length : 0)); checkMethod(method); HttpURLConnection connection = null; try { Proxy proxy = Proxy.NO_PROXY; if (isProxySet) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(mNetwork.getByName(proxyHost), proxyPort)); } final URL url = new URL(urlString); maybeWaitForIpv4(requestId, url); connection = (HttpURLConnection) mNetwork.openConnection(url, proxy); connection.setDoInput(true); connection.setConnectTimeout( mmsConfig.getInt(SmsManager.MMS_CONFIG_HTTP_SOCKET_TIMEOUT)); connection.setRequestProperty(HEADER_ACCEPT, HEADER_VALUE_ACCEPT); connection.setRequestProperty( HEADER_ACCEPT_LANGUAGE, getCurrentAcceptLanguage(Locale.getDefault())); final String userAgent = mmsConfig.getString(SmsManager.MMS_CONFIG_USER_AGENT); LogUtil.i(requestId, "HTTP: User-Agent=" + userAgent); connection.setRequestProperty(HEADER_USER_AGENT, userAgent); final String uaProfUrlTagName = mmsConfig.getString(SmsManager.MMS_CONFIG_UA_PROF_TAG_NAME); final String uaProfUrl = mmsConfig.getString(SmsManager.MMS_CONFIG_UA_PROF_URL); if (uaProfUrl != null) { LogUtil.i(requestId, "HTTP: UaProfUrl=" + uaProfUrl); connection.setRequestProperty(uaProfUrlTagName, uaProfUrl); } if (mmsConfig.getBoolean(SmsManager.MMS_CONFIG_CLOSE_CONNECTION, false)) { LogUtil.i(requestId, "HTTP: Connection close after request"); connection.setRequestProperty(HEADER_CONNECTION, HEADER_CONNECTION_CLOSE); } addExtraHeaders(connection, mmsConfig, subId); if (METHOD_POST.equals(method)) { if (pdu == null || pdu.length < 1) { LogUtil.e(requestId, "HTTP: empty pdu"); throw new MmsHttpException(0, "Sending empty PDU"); } connection.setDoOutput(true); connection.setRequestMethod(METHOD_POST); if (mmsConfig.getBoolean(SmsManager.MMS_CONFIG_SUPPORT_HTTP_CHARSET_HEADER)) { connection.setRequestProperty(HEADER_CONTENT_TYPE, HEADER_VALUE_CONTENT_TYPE_WITH_CHARSET); } else { connection.setRequestProperty(HEADER_CONTENT_TYPE, HEADER_VALUE_CONTENT_TYPE_WITHOUT_CHARSET); } if (LogUtil.isLoggable(Log.VERBOSE)) { logHttpHeaders(connection.getRequestProperties(), requestId); } connection.setFixedLengthStreamingMode(pdu.length); final OutputStream out = new BufferedOutputStream(connection.getOutputStream()); out.write(pdu); out.flush(); out.close(); } else if (METHOD_GET.equals(method)) { if (LogUtil.isLoggable(Log.VERBOSE)) { logHttpHeaders(connection.getRequestProperties(), requestId); } connection.setRequestMethod(METHOD_GET); } final int responseCode = connection.getResponseCode(); final String responseMessage = connection.getResponseMessage(); LogUtil.d(requestId, "HTTP: " + responseCode + " " + responseMessage); if (LogUtil.isLoggable(Log.VERBOSE)) { logHttpHeaders(connection.getHeaderFields(), requestId); } if (responseCode / 100 != 2) { throw new MmsHttpException(responseCode, responseMessage); } final InputStream in = new BufferedInputStream(connection.getInputStream()); final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); final byte[] buf = new byte[4096]; int count = 0; while ((count = in.read(buf)) > 0) { byteOut.write(buf, 0, count); } in.close(); final byte[] responseBody = byteOut.toByteArray(); LogUtil.d(requestId, "HTTP: response size=" + (responseBody != null ? responseBody.length : 0)); return responseBody; } catch (MalformedURLException e) { final String redactedUrl = redactUrlForNonVerbose(urlString); LogUtil.e(requestId, "HTTP: invalid URL " + redactedUrl, e); throw new MmsHttpException(0, "Invalid URL " + redactedUrl, e); } catch (ProtocolException e) { final String redactedUrl = redactUrlForNonVerbose(urlString); LogUtil.e(requestId, "HTTP: invalid URL protocol " + redactedUrl, e); throw new MmsHttpException(0, "Invalid URL protocol " + redactedUrl, e); } catch (IOException e) { LogUtil.e(requestId, "HTTP: IO failure", e); throw new MmsHttpException(0, e); } finally { if (connection != null) { connection.disconnect(); } } }
|