Java Code Examples for java.net.HttpURLConnection#setDoInput()

The following examples show how to use java.net.HttpURLConnection#setDoInput() . 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: AbstractCatalinaTask.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void preAuthenticate() throws IOException {
    URLConnection conn = null;

    // Create a connection for this command
    conn = (new URL(url)).openConnection();
    HttpURLConnection hconn = (HttpURLConnection) conn;

    // Set up standard connection characteristics
    hconn.setAllowUserInteraction(false);
    hconn.setDoInput(true);
    hconn.setUseCaches(false);
    hconn.setDoOutput(false);
    hconn.setRequestMethod("OPTIONS");
    hconn.setRequestProperty("User-Agent", "Catalina-Ant-Task/1.0");

    // Establish the connection with the server
    hconn.connect();

    // Swallow response message
    IOTools.flow(hconn.getInputStream(), null);
}
 
Example 2
Source File: GnocchiQuery.java    From hawkular-alerts with Apache License 2.0 6 votes vote down vote up
private List<Map<String, String>> getMetrics(List<String> gnocchiNames) {
    String metricsUrl = baseUrl + "/v1/metric";
    List<Map<String, String>> rawMetrics = new ArrayList<>();
    if (isEmpty(gnocchiNames)) {
        return rawMetrics;
    }
    for (String metricName : gnocchiNames) {
        String metricUrl = metricsUrl + "?name=" + metricName;
        try {
            URL url = new URL(metricUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty(AUTHORIZATION, basicAuth);
            conn.setRequestMethod(GET);
            conn.setDoInput(true);
            List rawMetric = JsonUtil.getMapper().readValue(conn.getInputStream(), List.class);
            conn.disconnect();
            log.debugf("Gnocchi Metrics found %s", rawMetric);
            rawMetrics.addAll(rawMetric);
        } catch (IOException e) {
            log.errorf(e,"Error querying Gnocchi metrics %s", metricUrl);
        }
    }
    return rawMetrics;
}
 
Example 3
Source File: MultipartUtility.java    From opencps-v2 with GNU Affero General Public License v3.0 6 votes vote down vote up
public MultipartUtility(String requestURL, String charset, long groupId, String authStringEnc, String method)
		throws IOException {
	this.charset = charset;

	// creates a unique boundary based on time stamp
	boundary = "===" + System.currentTimeMillis() + "===";

	URL url = new URL(requestURL);
	httpConn = (HttpURLConnection) url.openConnection();
	httpConn.setUseCaches(false);
	httpConn.setDoOutput(true); // indicates POST method
	httpConn.setDoInput(true);
	httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
	httpConn.setRequestProperty("User-Agent", "OpenCPS-Agent");

	httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);

	httpConn.setRequestMethod(method);
	httpConn.setDoInput(true);
	httpConn.setDoOutput(true);
	httpConn.setRequestProperty("Accept", "application/json");
	httpConn.setRequestProperty("groupId", String.valueOf(groupId));

	outputStream = httpConn.getOutputStream();
	writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
 
Example 4
Source File: CordovaResourceApi.java    From wildfly-samples with MIT License 6 votes vote down vote up
public String getMimeType(Uri uri) {
    switch (getUriType(uri)) {
        case URI_TYPE_FILE:
        case URI_TYPE_ASSET:
            return getMimeTypeFromPath(uri.getPath());
        case URI_TYPE_CONTENT:
        case URI_TYPE_RESOURCE:
            return contentResolver.getType(uri);
        case URI_TYPE_DATA: {
            return getDataUriMimeType(uri);
        }
        case URI_TYPE_HTTP:
        case URI_TYPE_HTTPS: {
            try {
                HttpURLConnection conn = httpClient.open(new URL(uri.toString()));
                conn.setDoInput(false);
                conn.setRequestMethod("HEAD");
                return conn.getHeaderField("Content-Type");
            } catch (IOException e) {
            }
        }
    }
    
    return null;
}
 
Example 5
Source File: HurlStack.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
/**
 * Opens an {@link HttpURLConnection} with parameters.
 * 
 * @param url
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request)
                throws IOException {

    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
        ((HttpsURLConnection) connection)
                        .setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
}
 
Example 6
Source File: HTTPRequest.java    From AndroidQuick with MIT License 6 votes vote down vote up
private HTTPResponse post() throws IOException {
    URL url = new URL(mPath);
    HttpURLConnection lConnection = (HttpURLConnection) url.openConnection();

    lConnection.setReadTimeout(mReadTimeoutMillis);
    lConnection.setConnectTimeout(mConnectTimeoutMillis);
    lConnection.setRequestMethod("POST");
    lConnection.setDoInput(true);
    lConnection.setDoOutput(true);

    OutputStream lOutStream = lConnection.getOutputStream();
    BufferedWriter lWriter = new BufferedWriter(new OutputStreamWriter(lOutStream, "UTF-8"));
    lWriter.write(getQuery(mParameters));
    lWriter.flush();
    lWriter.close();
    lOutStream.close();

    HTTPResponse response = readPage(lConnection);

    return response;
}
 
Example 7
Source File: HurlStack.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @param url
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
        ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
}
 
Example 8
Source File: DefaultWXHttpAdapter.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * Opens an {@link HttpURLConnection} with parameters.
 *
 * @param request
 * @param listener
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(WXRequest request, OnHttpListener listener) throws IOException {
  URL url = new URL(request.url);
  HttpURLConnection connection = createConnection(url);
  connection.setConnectTimeout(request.timeoutMs);
  connection.setReadTimeout(request.timeoutMs);
  connection.setUseCaches(false);
  connection.setDoInput(true);

  if (request.paramMap != null) {
    Set<String> keySets = request.paramMap.keySet();
    for (String key : keySets) {
      connection.addRequestProperty(key, request.paramMap.get(key));
    }
  }

  if ("POST".equals(request.method)) {
    connection.setRequestMethod("POST");
    if (request.body != null) {
      if (listener != null) {
        listener.onHttpUploadProgress(0);
      }
      connection.setDoOutput(true);
      DataOutputStream out = new DataOutputStream(connection.getOutputStream());
      //TODO big stream will cause OOM; Progress callback is meaningless
      out.write(request.body.getBytes());
      out.close();
      if (listener != null) {
        listener.onHttpUploadProgress(100);
      }
    }
  } else if (!TextUtils.isEmpty(request.method)) {
    connection.setRequestMethod(request.method);
  } else {
    connection.setRequestMethod("GET");
  }

  return connection;
}
 
Example 9
Source File: SyncCall.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
private HttpURLConnection openConnection(URL url, Request request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    connection.setConnectTimeout(mHttpHelper.mConnectTimeOut);
    connection.setReadTimeout(mHttpHelper.mReadTimeOut);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    if ("https".equals(url.getProtocol()) && mHttpHelper.mSslSocketFactory != null) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(mHttpHelper.mSslSocketFactory);
    }

    return connection;
}
 
Example 10
Source File: RequestExecutionHelper.java    From cloud-espm-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Run a http post request
 * 
 * @param entityName
 * @param body
 * @return http response
 * @throws MalformedURLException
 * @throws IOException
 * @throws ProtocolException
 */
public static HttpResponse executePostRequest(String entityName, String body)
		throws MalformedURLException, IOException, ProtocolException {
	URL url = new URL(SERVICE_ROOT_URI + entityName);
	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	try {
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Accept",
				"application/atom+xml");
		connection.setRequestProperty("Content-Type",
				"application/atom+xml");
		DataOutputStream outStream = new DataOutputStream(
				connection.getOutputStream());
		try {
			outStream.writeBytes(body);
			outStream.close();
			return new HttpResponse(connection);
		} finally {
			outStream.close();
		}
	} finally {
		connection.disconnect();
	}
}
 
Example 11
Source File: JerseyBlockingTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
private static int postData( String path, String payload ) throws IOException {
  URL url = new URL( path );

  HttpURLConnection con = (HttpURLConnection)url.openConnection();
  con.setRequestMethod("POST");
  
  con.setRequestProperty("Content-Type","application/json");

  con.setDoOutput(true); 
  con.setDoInput(true); 
  con.setConnectTimeout( 10000 );
  con.setReadTimeout( 20000 );
  
  con.getOutputStream().write( payload.getBytes("UTF-8") );
  con.getOutputStream().flush();
  con.getOutputStream().close();
  
  int status = con.getResponseCode();
  
  if( status != 200 ) {
    return status;
  }
  
  //read the response
  byte[] buffer = new byte[ 1024 ];
  while( con.getInputStream().read( buffer ) > 0 ) {
      ;
  }
  
  return 200;
}
 
Example 12
Source File: HttpUtil.java    From wlmedia with Apache License 2.0 5 votes vote down vote up
private HttpURLConnection getHttpURLConnection(String uri, String referer, String method) throws IOException {

        URL url = new URL(uri);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        if(referer != null && "".equals(referer))
        {
            httpConn.setRequestProperty("Referer", referer);
        }
        httpConn.setRequestMethod(method);
        httpConn.setDoInput(true);
        return httpConn;
    }
 
Example 13
Source File: UUIDFetcher.java    From Modern-LWC with MIT License 5 votes vote down vote up
private static HttpURLConnection createConnection() throws Exception {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}
 
Example 14
Source File: HttpUtil.java    From myapplication with Apache License 2.0 5 votes vote down vote up
public static String getSearchJsonStr(String keywordStr) throws UnsupportedEncodingException {
//        String requestUrl = "http://gank.io/api/search/query/listview/category/"
//                + URLEncoder.encode(gankClassStr, "utf-8") + "/count/30/page/1 ";
        String requestUrl = "http://gank.io/api/search/query/"
                + URLEncoder.encode(keywordStr, "utf-8") + "/category/all/count/50/page/1";
        StringBuffer buffer = null;
        try {
            // 建立连接
            URL url = new URL(requestUrl);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            // 获取输入流
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            // 读取返回结果
            buffer = new StringBuffer();
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }

            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (buffer != null) {
            return buffer.toString();  //返回获取的json字符串
        } else {
            return "";
        }
    }
 
Example 15
Source File: OpenshiftStartedEnvironment.java    From pnc with Apache License 2.0 5 votes vote down vote up
private boolean connectToPingUrl(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(500);
    connection.setRequestMethod("GET");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.connect();

    int responseCode = connection.getResponseCode();
    connection.disconnect();

    logger.debug("Got {} from {}.", responseCode, url);
    return responseCode == 200;
}
 
Example 16
Source File: AuthEndpoint.java    From taiga-android with Apache License 2.0 5 votes vote down vote up
@Override
protected AuthResponse doInBackground(String... params) {
    try {
        URL url = new URL(taigaUrl + "/api/v1/auth");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        String postData = String.format("type=normal&username=%s&password=%s",
                URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(password, "UTF-8"));

        try (OutputStream os = conn.getOutputStream();
             OutputStreamWriter out = new OutputStreamWriter(os, "UTF-8");
             BufferedWriter writer = new BufferedWriter(out)) {
            writer.write(postData);
            writer.flush();
        }
        int responseCode = conn.getResponseCode();

        Log.w(TAG, "Login response code: " + responseCode);
        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String jsonResponse = IOUtils.toString(conn.getInputStream());
            Log.d(TAG, "Login response: " + jsonResponse);

            return new GsonBuilder().create().fromJson(jsonResponse, AuthResponse.class);
        }
        throw new IllegalStateException(String.valueOf(responseCode));
    } catch (Exception e) {
        Log.e(TAG, "Error requesting login", e);
        return null;
    }
}
 
Example 17
Source File: HttpUtil.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
 * @param url 请求URL
 * @param method 请求URL
 * @param param	json参数(post|put)
 * @return
 */
public static String urlRequest(String url,String method,String param,String auth){
	String result = null;
	try {
		HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
		connection.setConnectTimeout(60*1000);
		connection.setRequestMethod(method.toUpperCase());
		if(auth!=null&&!"".equals(auth)){
			String authorization = "Basic "+new String(Base64.encodeBase64(auth.getBytes()));
			connection.setRequestProperty("Authorization", authorization);
		}
		if(param!=null&&!"".equals(param)){
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.connect();
			DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
			dos.write(param.getBytes(Consts.UTF_8));
			dos.flush();
			dos.close();
		}else{
			connection.connect();
		}
		if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
			InputStream in = connection.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int len = 0;
			while((len=in.read(buff, 0, buff.length))>0){
				out.write(buff, 0, len);
			}
			byte[] data = out.toByteArray();
			in.close();
			result = data!=null&&data.length>0?new String(data, Consts.UTF_8):null;
		}else{
			result = "{\"status\":"+connection.getResponseCode()+",\"msg\":\""+connection.getResponseMessage()+"\"}";
		}
		connection.disconnect();
	}catch (Exception e) {
		logger.error("--http request error !",e);
	}
	return result;
}
 
Example 18
Source File: KerberosWebHDFSConnection2.java    From Transwarp-Sample-Code with MIT License 4 votes vote down vote up
/**
 * <b>CREATE</b>
 *
 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=CREATE
 * [&overwrite=<true|false>][&blocksize=<LONG>][&replication=<SHORT>]
 * [&permission=<OCTAL>][&buffersize=<INT>]"
 *
 * @param path
 * @param is
 * @return
 * @throws MalformedURLException
 * @throws IOException
 * @throws AuthenticationException
 */
public String createPOC(String path, InputStream is, HashMap<String, String> map)
        throws MalformedURLException, IOException, AuthenticationException {
    String resp = null;
    ensureValidToken();

    String redirectUrl = null;
    HttpURLConnection conn = authenticatedURL
            .openConnection(
                    new URL(new URL(httpfsUrl), MessageFormat.format(
                            "/webhdfs/v1/{0}?op=CREATE",
                            URLUtil.encodePath(path))), token);
    conn.setRequestMethod("PUT");
    conn.setInstanceFollowRedirects(false);
    conn.connect();
    logger.info("Location:" + conn.getHeaderField("Location"));
    System.out.println("Location:" + conn.getHeaderField("Location"));
    resp = result(conn, true);
    if (conn.getResponseCode() == 307){
        String str = conn.getHeaderField("Location");
        if(str.startsWith("http://big1.big")){
            redirectUrl = str.replaceAll("big1.big", map.get("big1.big"));
        }else if(str.startsWith("http://big2.big")){
            redirectUrl = str.replaceAll("big2.big", map.get("big2.big"));
        }else if(str.startsWith("http://big3.big")){
            redirectUrl = str.replaceAll("big3.big", map.get("big3.big"));
        }else{
            redirectUrl = conn.getHeaderField("Location");
        }
    }
    conn.disconnect();

    if (redirectUrl != null) {
        conn = authenticatedURL.openConnection(new URL(redirectUrl), token);
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setRequestProperty("Content-Type", "application/octet-stream");
        // conn.setRequestProperty("Transfer-Encoding", "chunked");
        final int _SIZE = is.available();
        conn.setRequestProperty("Content-Length", "" + _SIZE);
        conn.setFixedLengthStreamingMode(_SIZE);
        conn.connect();
        OutputStream os = conn.getOutputStream();
        copy(is, os);
        // Util.copyStream(is, os);
        is.close();
        os.close();
        resp = result(conn, false);
        conn.disconnect();
    }

    return resp;
}
 
Example 19
Source File: DefaultFileTranser.java    From pnc with Apache License 2.0 4 votes vote down vote up
@Override
public StringBuffer downloadFileToStringBuilder(StringBuffer logsAggregate, URI uri) throws TransferException {
    try {
        logger.debug("Downloading file to String Buffer from {}", uri);

        ArrayDeque<String> logLines = new ArrayDeque<>();

        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
        connection.setRequestMethod("GET");

        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setConnectTimeout(connectTimeout);
        connection.setReadTimeout(readTimeout);

        Consumer<String> removedLines = (removedLine) -> {
            fullyDownloaded = false;
            logger.debug("Dropped log line from URI {}: {}.", uri, removedLine);
        };

        try (InputStream inputStream = connection.getInputStream()) {
            Charset charset = Charset.forName(ENCODING);
            StringUtils.readStream(inputStream, charset, logLines, maxDownloadSize, removedLines);
        }

        logsAggregate.append("==== ").append(uri.toString()).append(" ====\n");
        while (true) {
            String line = logLines.pollFirst();
            if (line == null) {
                break;
            }
            logsAggregate.append(line + "\n");
        }
        if (logLines.size() > 0) {
            logger.warn("Log buffer was not fully drained for URI: {}", uri);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Downloaded log: {}.", logsAggregate);
        }
        return logsAggregate;
    } catch (IOException e) {
        throw new TransferException("Could not obtain log file: " + uri.toString(), e);
    }
}
 
Example 20
Source File: SiteMembershipsSynchroniserImpl.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private final void synchronizeLTI1SiteMemberships(final Site site, final String membershipsId, final String membershipsUrl, final String oauth_consumer_key, boolean isEmailTrustedConsumer) {

        // Lookup the secret
        final String configPrefix = "basiclti.provider." + oauth_consumer_key + ".";
        final String oauth_secret = serverConfigurationService.getString(configPrefix+ "secret", null);
        if (oauth_secret == null) {
            log.error("launch.key.notfound {}. This site's memberships will NOT be synchronised.", oauth_consumer_key);
            return;
        }

        OAuthMessage om = new OAuthMessage("POST", membershipsUrl, null);
        om.addParameter(OAuth.OAUTH_CONSUMER_KEY, oauth_consumer_key);
        om.addParameter(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
        om.addParameter(OAuth.OAUTH_VERSION, "1.0");
        om.addParameter(OAuth.OAUTH_TIMESTAMP, new Long((new Date().getTime()) / 1000).toString());
        om.addParameter(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
        om.addParameter(BasicLTIConstants.LTI_MESSAGE_TYPE, "basic-lis-readmembershipsforcontext");
        om.addParameter(BasicLTIConstants.LTI_VERSION, "LTI-1p0");
        om.addParameter("id", membershipsId);

        OAuthConsumer oc = new OAuthConsumer(null, oauth_consumer_key, oauth_secret, null);

        try {
            OAuthSignatureMethod osm = OAuthSignatureMethod.newMethod(OAuth.HMAC_SHA1, new OAuthAccessor(oc));
            osm.sign(om);

            URL url = new URL(membershipsUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(false); 
            connection.setRequestMethod("POST");
            connection.setUseCaches (false);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            bw.write(OAuth.formEncode(om.getParameters()));
            bw.flush();
            bw.close();

            processMembershipsResponse(connection, site, oauth_consumer_key, isEmailTrustedConsumer);
        } catch (Exception e) {
            log.warn("Problem synchronizing LTI1 memberships.", e);
        }
    }