Java Code Examples for java.net.httpurlconnection#getResponseMessage()

The following examples show how to use java.net.httpurlconnection#getResponseMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LamsSecurityUtil.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
    * Make a call to LAMS server.
    * 
    * @param serviceURL
    * @return resulted InputStream
    * @throws IOException
    */
   private static InputStream callLamsServer(String serviceURL) throws IOException {
URL url = new URL(serviceURL);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection)) {
    throw new IOException("Unable to open connection to: " + serviceURL);
}

HttpURLConnection httpConn = (HttpURLConnection) conn;

if (httpConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new IOException("LAMS server responded with HTTP response code: " + httpConn.getResponseCode()
	    + ", HTTP response message: " + httpConn.getResponseMessage());
}

// InputStream is = url.openConnection().getInputStream();
InputStream is = conn.getInputStream();

return is;
   }
 
Example 2
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void setupFromConnection(HttpURLConnection conn, Connection.Response previousResponse) throws IOException {
    method = Method.valueOf(conn.getRequestMethod());
    url = conn.getURL();
    statusCode = conn.getResponseCode();
    statusMessage = conn.getResponseMessage();
    contentType = conn.getContentType();

    Map<String, List<String>> resHeaders = createHeaderMap(conn);
    processResponseHeaders(resHeaders);

    // if from a redirect, map previous response cookies into this response
    if (previousResponse != null) {
        for (Map.Entry<String, String> prevCookie : previousResponse.cookies().entrySet()) {
            if (!hasCookie(prevCookie.getKey()))
                cookie(prevCookie.getKey(), prevCookie.getValue());
        }
    }
}
 
Example 3
Source File: FlickrFetchr.java    From AndroidProgramming3e with Apache License 2.0 6 votes vote down vote up
public byte[] getUrlBytes(String urlSpec) throws IOException {
    URL url = new URL(urlSpec);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = connection.getInputStream();
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException(connection.getResponseMessage() +
                    ": with " +
                    urlSpec);
        }
        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        out.close();
        return out.toByteArray();
    } finally {
        connection.disconnect();
    }
}
 
Example 4
Source File: HttpWebResponse.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void readResponseHeader( HttpURLConnection connection ) throws IOException {
    if (!needStatusWorkaround()) {
        _responseCode = connection.getResponseCode();
        _responseMessage = connection.getResponseMessage();
    } else {
         if (connection.getHeaderField(0) == null) throw new UnknownHostException( connection.getURL().toExternalForm() );

        StringTokenizer st = new StringTokenizer( connection.getHeaderField(0) );
        st.nextToken();
        if (!st.hasMoreTokens()) {
            _responseCode = HttpURLConnection.HTTP_OK;
            _responseMessage = "OK";
        } else try {
            _responseCode = Integer.parseInt( st.nextToken() );
            _responseMessage = getRemainingTokens( st );
        } catch (NumberFormatException e) {
            _responseCode = HttpURLConnection.HTTP_INTERNAL_ERROR;
            _responseMessage = "Cannot parse response header";
        }
    }
}
 
Example 5
Source File: AuthManager.java    From EggCrack with GNU General Public License v2.0 5 votes vote down vote up
private String performJsonPost(URL url, String json, Proxy currentProxy) throws IOException {
/*     */     try {
/*  61 */       HttpURLConnection conn = (HttpURLConnection)url.openConnection(currentProxy);
/*  62 */       byte[] payloadAsBytes = json.getBytes(Charset.forName("UTF-8"));
/*     */       
/*  64 */       conn.setConnectTimeout(1200);
/*  65 */       conn.setReadTimeout(10000);
/*  66 */       conn.setRequestMethod("POST");
/*  67 */       conn.setRequestProperty("User-Agent", "MCU-Yggdrasil/1.0");
/*  68 */       conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
/*  69 */       conn.setRequestProperty("Content-Length", "" + payloadAsBytes.length);
/*  70 */       conn.setRequestProperty("Content-Language", "en-US");
/*  71 */       conn.setUseCaches(false);
/*  72 */       conn.setDoInput(true);
/*  73 */       conn.setDoOutput(true);
/*     */       
/*  75 */       DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
/*  76 */       outStream.write(payloadAsBytes);
/*  77 */       outStream.flush();
/*  78 */       outStream.close();

                InputStream inStream = null;
                if (conn.getResponseCode() / 100 != 2) inStream = conn.getErrorStream();
                else inStream = conn.getInputStream();

                if (inStream == null) throw new IOException(conn.getResponseMessage());
/*     */       
/*  87 */       StringBuilder response = new StringBuilder();
/*  88 */       byte[] buffer = new byte[2048];
/*     */       int bytesRead;
/*  90 */       while ((bytesRead = inStream.read(buffer)) > 0) {
/*  91 */         response.append(new String(buffer, "UTF-8").substring(0, bytesRead));
/*     */       }
        System.out.println(response.toString());
/*  93 */       return response.toString();
/*     */     }
/*     */     catch (IOException e) {
/*  96 */       throw e;
/*     */     }
/*     */   }
 
Example 6
Source File: AlipayMobilePublicMultiMediaClient.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
    String charset = getResponseCharset(conn.getContentType());
    InputStream es = conn.getErrorStream();
    if (es == null) {
        return getStreamAsString(conn.getInputStream(), charset);
    } else {
        String msg = getStreamAsString(es, charset);
        if (StringUtils.isEmpty(msg)) {
            throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else {
            throw new IOException(msg);
        }
    }
}
 
Example 7
Source File: UrlConnectionHttpFunction.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
private HttpResponse getHttpResponseResult(final @NonNull HttpRequest request,
    @NonNull final HttpURLConnection connection) throws IOException {
  connection.setConnectTimeout(request.connectTimeoutMs);
  connection.setReadTimeout(request.readTimeoutMs);
  connection.setInstanceFollowRedirects(request.followRedirects);
  connection.setUseCaches(request.useCaches);
  connection.setDoInput(true);
  connection.setRequestMethod(request.method);
  for (final Entry<String, String> headerField : request.header.entrySet()) {
    connection.addRequestProperty(headerField.getKey(), headerField.getValue());
  }
  final byte[] body = request.body;
  if (body.length > 0) {
    connection.setDoOutput(true);
    final OutputStream out = connection.getOutputStream();
    try {
      out.write(body);
    } finally {
      out.close();
    }
  }
  final String responseMessage = connection.getResponseMessage();
  return httpResponse(connection.getResponseCode(),
      responseMessage != null ? responseMessage : "",
      getHeader(connection), getByteArray(connection));
}
 
Example 8
Source File: HttpTools.java    From pgptool with GNU General Public License v3.0 5 votes vote down vote up
public static String httpGet(String url, Map<String, String> headers) {
	try {
		URL obj = new URL(url);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();

		// optional default is GET
		con.setRequestMethod("GET");

		// add request header
		if (headers != null) {
			for (Entry<String, String> header : headers.entrySet()) {
				con.setRequestProperty(header.getKey(), header.getValue());
			}
		}

		int responseCode = con.getResponseCode();
		if (200 != responseCode) {
			throw new IllegalStateException(
					"Unexpected response: " + responseCode + ": " + con.getResponseMessage());
		}

		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuilder response = new StringBuilder();
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
		return response.toString();
	} catch (Throwable t) {
		throw new RuntimeException("HTTP request failed: " + url, t);
	}
}
 
Example 9
Source File: HttpUtil.java    From craft-atom with MIT License 5 votes vote down vote up
private static String getResponseAsString(HttpURLConnection conn) throws IOException {
	String charset = getResponseCharset(conn.getContentType());
	InputStream es = conn.getErrorStream();
	if (es == null) {
		return getStreamAsString(conn.getInputStream(), charset);
	} else {
		String msg = getStreamAsString(es, charset);
		if (StringUtil.isEmpty(msg)) {
			throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
		} else {
			throw new IOException(msg);
		}
	}
}
 
Example 10
Source File: HttpsUtil.java    From xnx3 with Apache License 2.0 5 votes vote down vote up
/**
 * 得到响应对象
 * @param urlConnection
 * @param content 网页内容
 * @return 响应对象
 * @throws IOException
 */ 
private HttpResponse makeContent(String urlString, HttpURLConnection urlConnection, String content) throws IOException { 
    HttpResponse httpResponser = new HttpResponse(); 
    try { 
        httpResponser.contentCollection = new Vector<String>(); 
        String ecod = urlConnection.getContentEncoding(); 
        if (ecod == null) 
            ecod = this.encode; 
        httpResponser.urlString = urlString; 
        this.cookies=urlConnection.getHeaderField("Set-Cookie");
        httpResponser.cookie=this.cookies;
        httpResponser.defaultPort = urlConnection.getURL().getDefaultPort(); 
        httpResponser.file = urlConnection.getURL().getFile(); 
        httpResponser.host = urlConnection.getURL().getHost(); 
        httpResponser.path = urlConnection.getURL().getPath(); 
        httpResponser.port = urlConnection.getURL().getPort(); 
        httpResponser.protocol = urlConnection.getURL().getProtocol(); 
        httpResponser.query = urlConnection.getURL().getQuery(); 
        httpResponser.ref = urlConnection.getURL().getRef(); 
        httpResponser.userInfo = urlConnection.getURL().getUserInfo(); 
        httpResponser.content = content;
        httpResponser.contentEncoding = ecod; 
        httpResponser.code = urlConnection.getResponseCode(); 
        httpResponser.message = urlConnection.getResponseMessage(); 
        httpResponser.contentType = urlConnection.getContentType(); 
        httpResponser.method = urlConnection.getRequestMethod(); 
        httpResponser.connectTimeout = urlConnection.getConnectTimeout(); 
        httpResponser.readTimeout = urlConnection.getReadTimeout(); 
        httpResponser.headerFields = urlConnection.getHeaderFields();
        return httpResponser; 
    } catch (IOException e) { 
        throw e; 
    } finally { 
        if (urlConnection != null) 
            urlConnection.disconnect(); 
    } 
}
 
Example 11
Source File: HalyardElasticIndexer.java    From Halyard with Apache License 2.0 5 votes vote down vote up
private void export(boolean flush) throws IOException {
    if (literals.size() > 0) {
        batch.append("{\"index\":{\"_id\":\"").append(Hex.encodeHex(lastHash)).append("\"}}\n{");
        for (int i = 0; i < literals.size(); i++) {
            if (i > 0) {
                batch.append(',');
            }
            batch.append('\"').append(attr).append("\":").append(JSONObject.quote(literals.get(i)));
        }
        batch.append("}\n");
        literals.clear();
        exports++;
    }
    if ((flush && batch.length() > 0) || batch.length() > bufferLimit) {
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        http.setRequestMethod("POST");
        http.setDoOutput(true);
        http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        byte b[] = batch.toString().getBytes(StandardCharsets.UTF_8);
        batch = new StringBuilder();
        http.setFixedLengthStreamingMode(b.length);
        http.connect();
        try {
            try (OutputStream post = http.getOutputStream()) {
                post.write(b);
            }
            int response = http.getResponseCode();
            String msg = http.getResponseMessage();
            if (exports > 0 && response != 200) {
                LOG.severe(IOUtils.toString(http.getErrorStream()));
                throw new IOException(msg);
            }
            batches++;
        } finally {
            http.disconnect();
        }
    }
}
 
Example 12
Source File: HurlStack.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}
 
Example 13
Source File: WebUtils.java    From pay with Apache License 2.0 5 votes vote down vote up
protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
    String charset = getResponseCharset(conn.getContentType());
    InputStream es = conn.getErrorStream();
    if (es == null) {
        return getStreamAsString(conn.getInputStream(), charset);
    } else {
        String msg = getStreamAsString(es, charset);
        if (StringUtils.isEmpty(msg)) {
            throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else {
            throw new IOException(msg);
        }
    }
}
 
Example 14
Source File: HurlStack.java    From okulus with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion,
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}
 
Example 15
Source File: URLite.java    From httplite with Apache License 2.0 4 votes vote down vote up
static Response createResponse(HttpURLConnection urlConnection, Request request) throws IOException {
    ResponseBody body = createResponseBody(urlConnection);
    return new ResponseImpl(request, urlConnection.getResponseCode(), urlConnection.getResponseMessage(),
            urlConnection.getHeaderFields(), body);
}
 
Example 16
Source File: OkHttpStack.java    From something.apk with MIT License 4 votes vote down vote up
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTPS", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion,
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    URL finalUrl = connection.getURL();
    if(!parsedUrl.equals(finalUrl) && request instanceof Redirectable){
        ((Redirectable) request).onRedirect(finalUrl.toExternalForm());
    }
    return response;
}
 
Example 17
Source File: CmpAgent.java    From xipki with Apache License 2.0 4 votes vote down vote up
private byte[] send(byte[] request) throws IOException {
  Args.notNull(request, "request");
  HttpURLConnection httpUrlConnection = IoUtil.openHttpConn(serverUrl);
  if (httpUrlConnection instanceof HttpsURLConnection) {
    if (sslSocketFactory != null) {
      ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(sslSocketFactory);
    }

    if (hostnameVerifier != null) {
      ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(hostnameVerifier);
    }
  }

  httpUrlConnection.setDoOutput(true);
  httpUrlConnection.setUseCaches(false);

  int size = request.length;

  httpUrlConnection.setRequestMethod("POST");
  httpUrlConnection.setRequestProperty("Content-Type", CMP_REQUEST_MIMETYPE);
  httpUrlConnection.setRequestProperty("Content-Length", java.lang.Integer.toString(size));
  OutputStream outputstream = httpUrlConnection.getOutputStream();
  outputstream.write(request);
  outputstream.flush();

  InputStream inputStream = httpUrlConnection.getInputStream();
  if (httpUrlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    inputStream.close();
    throw new IOException("bad response: " + httpUrlConnection.getResponseCode() + "    "
            + httpUrlConnection.getResponseMessage());
  }

  String responseContentType = httpUrlConnection.getContentType();
  boolean isValidContentType = false;
  if (responseContentType != null) {
    if (responseContentType.equalsIgnoreCase(CMP_RESPONSE_MIMETYPE)) {
      isValidContentType = true;
    }
  }

  if (!isValidContentType) {
    inputStream.close();
    throw new IOException("bad response: mime type " + responseContentType + " not supported!");
  }

  return IoUtil.read(inputStream);
}
 
Example 18
Source File: UrlAccountOutput.java    From EggCrack with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void save(AuthenticatedAccount account) throws IOException {
    String query = String.format(
            "username=%s&password=%s&uuid=%s&name=%s&instance=%s&version=%s",

            URLEncoder.encode(account.getUsername(), CHARSET),
            URLEncoder.encode(account.getCredential().toString(), CHARSET),
            URLEncoder.encode(account.getUuid().toString(), CHARSET),
            URLEncoder.encode(account.getAccountName().toString(), CHARSET),

            URLEncoder.encode(INSTANCE_UUID.toString(), CHARSET),
            URLEncoder.encode(Integer.toString(EggCrack.getInstance().getVersion()), CHARSET)
    );

    synchronized (url) {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(10000);
        urlConnection.setReadTimeout(10000);

        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Accept-Charset", CHARSET);
        urlConnection.setRequestProperty("User-Agent", "EggCrack");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + CHARSET);

        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setInstanceFollowRedirects(false);

        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(query.getBytes(CHARSET));
        outputStream.flush();

        if (urlConnection.getResponseCode() / 100 != 2)
            throw new IOException("Request failed (HTTP " + urlConnection.getResponseCode() + "): "
                    + urlConnection.getResponseMessage());

        EggCrack.LOGGER.fine("Account " + account.getUsername()
                + " submitted to URL \"" + url.toExternalForm() + "\".");

        //Safely close the connection.
        urlConnection.getInputStream().close();
    }
}
 
Example 19
Source File: UrlFetchWebConnection.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
     * {@inheritDoc}
     */
    @Override
    public WebResponse getResponse(final WebRequest webRequest) throws IOException {
        final long startTime = System.currentTimeMillis();
        final URL url = webRequest.getUrl();
        if (LOG.isTraceEnabled()) {
            LOG.trace("about to fetch URL " + url);
        }

        // hack for JS, about, and data URLs.
        final WebResponse response = produceWebResponseForGAEProcolHack(url);
        if (response != null) {
            return response;
        }

        // this is a "normal" URL
        try {
            final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.setUseCaches(false);
            connection.setConnectTimeout(webClient_.getOptions().getTimeout());

            connection.addRequestProperty(HttpHeader.USER_AGENT, webClient_.getBrowserVersion().getUserAgent());
            connection.setInstanceFollowRedirects(false);

            // copy the headers from WebRequestSettings
            // this might not copy all headers because the JDK ignores/censors some headers
            // you can disable this by setting sun.net.http.allowRestrictedHeaders to true
            // see UrlFetchWebConnectionTest for some links
            for (final Entry<String, String> header : webRequest.getAdditionalHeaders().entrySet()) {
                connection.addRequestProperty(header.getKey(), header.getValue());
            }
            addCookies(connection);

            final HttpMethod httpMethod = webRequest.getHttpMethod();
            connection.setRequestMethod(httpMethod.name());
            if (HttpMethod.POST == httpMethod || HttpMethod.PUT == httpMethod || HttpMethod.PATCH == httpMethod) {
                connection.setDoOutput(true);
                final Charset charset = webRequest.getCharset();
                connection.addRequestProperty(HttpHeader.CONTENT_TYPE, FormEncodingType.URL_ENCODED.getName());

                try (OutputStream outputStream = connection.getOutputStream()) {
                    final List<NameValuePair> pairs = webRequest.getRequestParameters();
                    final cz.msebera.android.httpclient.NameValuePair[] httpClientPairs = NameValuePair.toHttpClient(pairs);
                    final String query = URLEncodedUtils.format(Arrays.asList(httpClientPairs), charset);
                    outputStream.write(query.getBytes(charset));
                    if (webRequest.getRequestBody() != null) {
                        IOUtils.write(webRequest.getRequestBody().getBytes(charset), outputStream);
                    }
                }
            }

            final int responseCode = connection.getResponseCode();
            if (LOG.isTraceEnabled()) {
                LOG.trace("fetched URL " + url);
            }

            final List<NameValuePair> headers = new ArrayList<>();
            for (final Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
                final String headerKey = headerEntry.getKey();
                if (headerKey != null) { // map contains entry like (null: "HTTP/1.1 200 OK")
                    final StringBuilder sb = new StringBuilder();
                    for (final String headerValue : headerEntry.getValue()) {
                        if (sb.length() != 0) {
                            sb.append(", ");
                        }
                        sb.append(headerValue);
                    }
                    headers.add(new NameValuePair(headerKey, sb.toString()));
                }
            }

            final byte[] byteArray;
            try (InputStream is = responseCode < 400
                    ? connection.getInputStream() : connection.getErrorStream()) {
                byteArray = IOUtils.toByteArray(is);
            }

            final long duration = System.currentTimeMillis() - startTime;
            final WebResponseData responseData = new WebResponseData(byteArray, responseCode,
                connection.getResponseMessage(), headers);
            saveCookies(url.getHost(), headers);
            return new WebResponse(responseData, webRequest, duration);
        }
        catch (final IOException e) {
            LOG.error("Exception while tyring to fetch " + url, e);
            throw new RuntimeException(e);
        }
    }
 
Example 20
Source File: StreamClientImpl.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
protected StreamResponseMessage createResponse(HttpURLConnection urlConnection, InputStream inputStream) throws Exception {

        if (urlConnection.getResponseCode() == -1) {
            log.warning("Received an invalid HTTP response: " + urlConnection.getURL());
            log.warning("Is your Cling-based server sending connection heartbeats with " +
                "RemoteClientInfo#isRequestCancelled? This client can't handle " +
                "heartbeats, read the manual.");
            return null;
        }

        // Status
        UpnpResponse responseOperation = new UpnpResponse(urlConnection.getResponseCode(), urlConnection.getResponseMessage());

        log.fine("Received response: " + responseOperation);

        // Message
        StreamResponseMessage responseMessage = new StreamResponseMessage(responseOperation);

        // Headers
        responseMessage.setHeaders(new UpnpHeaders(urlConnection.getHeaderFields()));

        // Body
        byte[] bodyBytes = null;
        InputStream is = null;
        try {
            is = inputStream;
            if (inputStream != null) bodyBytes = IO.readBytes(is);
        } finally {
            if (is != null)
                is.close();
        }

        if (bodyBytes != null && bodyBytes.length > 0 && responseMessage.isContentTypeMissingOrText()) {

            log.fine("Response contains textual entity body, converting then setting string on message");
            responseMessage.setBodyCharacters(bodyBytes);

        } else if (bodyBytes != null && bodyBytes.length > 0) {

            log.fine("Response contains binary entity body, setting bytes on message");
            responseMessage.setBody(UpnpMessage.BodyType.BYTES, bodyBytes);

        } else {
            log.fine("Response did not contain entity body");
        }

        log.fine("Response message complete: " + responseMessage);
        return responseMessage;
    }