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

The following examples show how to use java.net.URLConnection#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: SslTest.java    From AthenaServing with Apache License 2.0 7 votes vote down vote up
public String postRequest(String urlAddress,String args,int timeOut) throws Exception{
    URL url = new URL(urlAddress);
    if("https".equalsIgnoreCase(url.getProtocol())){
        SslUtils.ignoreSsl();
    }
    URLConnection u = url.openConnection();
    u.setDoInput(true);
    u.setDoOutput(true);
    u.setConnectTimeout(timeOut);
    u.setReadTimeout(timeOut);
    OutputStreamWriter osw = new OutputStreamWriter(u.getOutputStream(), "UTF-8");
    osw.write(args);
    osw.flush();
    osw.close();
    u.getOutputStream();
    return IOUtils.toString(u.getInputStream());
}
 
Example 2
Source File: CertificateValidator.java    From keycloak with Apache License 2.0 7 votes vote down vote up
private Collection<X509CRL> loadFromURI(CertificateFactory cf, URI remoteURI) throws GeneralSecurityException {
    try {
        logger.debugf("Loading CRL from %s", remoteURI.toString());

        URLConnection conn = remoteURI.toURL().openConnection();
        conn.setDoInput(true);
        conn.setUseCaches(false);
        X509CRL crl = loadFromStream(cf, conn.getInputStream());
        return Collections.singleton(crl);
    }
    catch(IOException ex) {
        logger.errorf(ex.getMessage());
    }
    return Collections.emptyList();

}
 
Example 3
Source File: SliceClient.java    From dawnsci with Eclipse Public License 1.0 6 votes vote down vote up
private T getImage() throws Exception {
	
	isFinished = false;
	try {
		Format format = urlBuilder.getFormat();
		if (!format.isImage()) {
			throw new Exception("Cannot get image with format set to "+format);
		}

		final URL url = urlBuilder.getSliceURL();
		URLConnection  conn = url.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);

		return (T)ImageIO.read(url.openStream());
	} finally {
		isFinished = true;
	}
}
 
Example 4
Source File: AbstractStreamer.java    From dawnsci with Eclipse Public License 1.0 6 votes vote down vote up
protected URLConnection init(URL url, long sleepTime, int cacheSize) throws Exception {

		URLConnection  conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);

        String contentType = conn.getContentType();
        if (!contentType.startsWith(Constants.MCONTENT_TYPE)) throw new Exception("getImages() may only be used with "+Constants.MCONTENT_TYPE+", not "+contentType);

        this.delimiter  = contentType.split("boundary=")[1];
		this.queue      = new LinkedBlockingQueue<T>(cacheSize); // TODO How many images can be in the queue?
		this.in         = new BufferedInputStream(conn.getInputStream());
		this.sleepTime  = sleepTime;

        return conn;
	}
 
Example 5
Source File: HttpRequestUtils.java    From wx-crawl with Apache License 2.0 6 votes vote down vote up
public static Document sendGetSogou(String url, Proxy proxy) throws Exception {
    URL realURL = new URL(url);
    URLConnection conn = realURL.openConnection(proxy);
    conn.setConnectTimeout(WxCrawlerConstant.RequestInfo.REQUEST_TIMEOUT);
    conn.setReadTimeout(WxCrawlerConstant.RequestInfo.REQUEST_TIMEOUT);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Connection", WxCrawlerConstant.RequestInfo.CONNECTION);
    conn.setRequestProperty("User-Agent", WxCrawlerConstant.RequestInfo.USER_AGENT);
    conn.setRequestProperty("Accept", WxCrawlerConstant.RequestInfo.ACCEPT);
    conn.setRequestProperty("Accept-Language", WxCrawlerConstant.RequestInfo.ACCEPT_LANGUAGE);
    conn.setRequestProperty("Host", WxCrawlerConstant.RequestInfo.SOGOU_HOST);
    conn.setRequestProperty("Upgrade-Insecure-Requests", WxCrawlerConstant.RequestInfo.UPGRADE_INSECURE_REQUESTS);
    conn.connect();
    return Jsoup.parse(conn.getInputStream(), WxCrawlerConstant.RequestInfo.CHARSET_NAME, url);
}
 
Example 6
Source File: Client.java    From servicemix with Apache License 2.0 6 votes vote down vote up
public void sendRequest() throws Exception {
    URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorldSecurity")
            .openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    OutputStream os = connection.getOutputStream();
    // Post the request file.
    InputStream fis = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/examples/cxf/request.xml");
    IOUtils.copy(fis, os);
    // Read the response.
    InputStream is = connection.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(is, baos);
    System.out.println("the response is =====>");
    System.out.println(baos.toString());
}
 
Example 7
Source File: PeopleManager.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
public static File getOwnerAvaterFile(Context context, String accountName, boolean network) {
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    Cursor cursor = databaseHelper.getOwner(accountName);
    String url = null;
    if (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex("avatar");
        if (!cursor.isNull(idx)) url = cursor.getString(idx);
    }
    cursor.close();
    databaseHelper.close();
    if (url == null) return null;
    String urlLastPart = url.replaceFirst(REGEX_SEARCH_USER_PHOTO, "");
    File file = new File(context.getCacheDir(), urlLastPart);
    if (!file.getParentFile().mkdirs() && file.exists()) {
        return file;
    }
    if (!network) return null;
    try {
        URLConnection conn = new URL(url).openConnection();
        conn.setDoInput(true);
        byte[] bytes = Utils.readStreamToEnd(conn.getInputStream());
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(bytes);
        outputStream.close();
        return file;
    } catch (Exception e) {
        Log.w(TAG, e);
        return null;
    }

}
 
Example 8
Source File: MultiPartFormOutputStream.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new <code>java.net.URLConnection</code> object from the
 * specified <code>java.net.URL</code>.  This is a convenience method
 * which will set the <code>doInput</code>, <code>doOutput</code>,
 * <code>useCaches</code> and <code>defaultUseCaches</code> fields to
 * the appropriate settings in the correct order.
 *
 * @return  a <code>java.net.URLConnection</code> object for the URL
 * @throws  java.io.IOException  on input/output errors
 */
public static URLConnection createConnection(URL url) throws java.io.IOException{
	URLConnection urlConn = url.openConnection();
	if(urlConn instanceof HttpURLConnection){
		HttpURLConnection httpConn = (HttpURLConnection)urlConn;
		httpConn.setRequestMethod("POST");
	}
	urlConn.setDoInput(true);
	urlConn.setDoOutput(true);
	urlConn.setUseCaches(false);
	urlConn.setDefaultUseCaches(false);
	return urlConn;
}
 
Example 9
Source File: UbuntuPaster.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Paste a text to paste.ubuntu.com
 *
 * @param text The text you want paste.
 * @return Target paste URL.
 * @throws Exception the throws
 */
@NotNull
public String pasteTheText(@NotNull String text) throws Exception {
    URL url = new URL("https://paste.ubuntu.com");
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty(
            "user-agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setConnectTimeout(50000);
    conn.setReadTimeout(100000);
    PrintWriter out = new PrintWriter(conn.getOutputStream());
    // poster=aaaaaaa&syntax=text&expiration=&content=%21%40
    String builder =
            "poster="
                    + "QuickShop Paster"
                    + "&syntax=text"
                    + "&expiration=week"
                    + "&content="
                    + URLEncoder.encode(text, "UTF-8");
    out.print(builder);
    out.flush(); // Drop
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    Util.debugLog("Request Completed: " + conn.getURL());
    String link = conn.getURL().toString();
    in.close();
    out.close();
    return link;
}
 
Example 10
Source File: XMLEntityManager.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static OutputStream createOutputStream(String uri) throws IOException {
    // URI was specified. Handle relative URIs.
    final String expanded = XMLEntityManager.expandSystemId(uri, null, true);
    final URL url = new URL(expanded != null ? expanded : uri);
    OutputStream out = null;
    String protocol = url.getProtocol();
    String host = url.getHost();
    // Use FileOutputStream if this URI is for a local file.
    if (protocol.equals("file")
            && (host == null || host.length() == 0 || host.equals("localhost"))) {
        File file = new File(getPathWithoutEscapes(url.getPath()));
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
        }
        out = new FileOutputStream(file);
    }
    // Try to write to some other kind of URI. Some protocols
    // won't support this, though HTTP should work.
    else {
        URLConnection urlCon = url.openConnection();
        urlCon.setDoInput(false);
        urlCon.setDoOutput(true);
        urlCon.setUseCaches(false); // Enable tunneling.
        if (urlCon instanceof HttpURLConnection) {
            // The DOM L3 REC says if we are writing to an HTTP URI
            // it is to be done with an HTTP PUT.
            HttpURLConnection httpCon = (HttpURLConnection) urlCon;
            httpCon.setRequestMethod("PUT");
        }
        out = urlCon.getOutputStream();
    }
    return out;
}
 
Example 11
Source File: FileServiceApp.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void doPost(String url, String content) throws IOException {
	URL urlObj = new URL(url);
	URLConnection conn = urlObj.openConnection();
	conn.setDoInput(true);
	conn.setDoOutput(true);
	conn.setUseCaches(false);
	conn.setRequestProperty("Content-Type",	"application/x-www-form-urlencoded");
	DataOutputStream out = new DataOutputStream(conn.getOutputStream());
	out.writeBytes(content);
	out.flush();
	out.close();
	conn.getInputStream().close();
}
 
Example 12
Source File: MillionFirstStepsBookMetadataJSONExtractor.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public String doUrl (URL url) throws IOException {
    StringBuilder sb = new StringBuilder(5000);
    
    if (url != null) {
        URLConnection con = url.openConnection();
        Wandora.initUrlConnection(con);
        con.setDoInput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-type", "text/plain");
            
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName(defaultEncoding)));

            String s;
            while ((s = in.readLine()) != null) {
                sb.append(s);
                if(!(s.endsWith("\n") || s.endsWith("\r"))) sb.append("\n");
            }
            in.close();
        } 
        catch (Exception ex) {
            log(ex);
        }
    }
    
    return sb.toString();
}
 
Example 13
Source File: YahooStockQuoteService.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public Collection<StockQuote> getStockQuotes ( final Collection<String> symbols )
{
    try
    {
        final URL url = generateURL ( symbols );
        final URLConnection connection = url.openConnection ();
        connection.setDoInput ( true );
        return parseResult ( symbols, connection.getInputStream () );
    }
    catch ( final Throwable e )
    {
        return failAll ( symbols, e );
    }

}
 
Example 14
Source File: BenchmarkClient.java    From lightstep-tracer-java with MIT License 5 votes vote down vote up
private InputStream getUrlReader(String path) {
    try {
        URL u = new URL(baseUrl + path);
        URLConnection c = u.openConnection();
        c.setDoOutput(true);
        c.setDoInput(true);
        c.getOutputStream().close();
        return c.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: DiscogsSearchExtractor.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public static String doUrl (URL url) throws IOException {
    StringBuilder sb = new StringBuilder(5000);
    
    if (url != null) {
       
            URLConnection con = url.openConnection();
            Wandora.initUrlConnection(con);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-type", "text/plain");
            
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName(defaultEncoding)));

            String s;
            while ((s = in.readLine()) != null) {
                sb.append(s);
                if(!(s.endsWith("\n") || s.endsWith("\r"))) sb.append("\n");
            }
            in.close();
        } catch (Exception ex) {
            System.out.println("There was an error fetching data from Discogs.");
        }
    }
    
    return sb.toString();
}
 
Example 16
Source File: SensUtils.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 百度主动推送
 *
 * @param blogUrl 博客地址
 * @param token   百度推送token
 * @param urls    文章路径
 * @return String
 */
public static String baiduPost(String blogUrl, String token, String urls) {
    String url = "http://data.zz.baidu.com/urls?site=" + blogUrl + "&token=" + token;
    String result = "";
    PrintWriter out = null;
    BufferedReader in = null;
    try {
        //建立URL之间的连接
        URLConnection conn = new URL(url).openConnection();
        //设置通用的请求属性
        conn.setRequestProperty("Host", "data.zz.baidu.com");
        conn.setRequestProperty("User-Agent", "curl/7.12.1");
        conn.setRequestProperty("Content-Length", "83");
        conn.setRequestProperty("Content-Type", "text/plain");

        //发送POST请求必须设置如下两行
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //获取conn对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        out.print(urls.trim());
        //进行输出流的缓冲
        out.flush();
        //通过BufferedReader输入流来读取Url的响应
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != out) {
                out.close();
            }
            if (null != in) {
                in.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return result;
}
 
Example 17
Source File: YouTubeBackgroundPlayback.java    From YouTubeBackgroundPlayback with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected JSONObject doInBackground(Void... params) {
	InputStream in = null;
	try {
		URLConnection conn = new URL(hooksDownloadUrl).openConnection();
		conn.setConnectTimeout(40 * 1000 /* ms */);
		conn.setDoInput(true);
		conn.setDoOutput(false);
		conn.setReadTimeout(20 * 1000 /* ms */);
		conn.setUseCaches(true);

		if (conn instanceof HttpURLConnection) {
			HttpURLConnection hConn = (HttpURLConnection) conn;
			hConn.setChunkedStreamingMode(0);
			hConn.setInstanceFollowRedirects(true);
			hConn.setRequestMethod("GET");
		} else {
			Log.w(LOG_TAG, "Our connection is not java.net.HttpURLConnection but instead " + conn.getClass().getName());
		}

		conn.connect();
		in = conn.getInputStream();

		BufferedReader inReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
		StringBuilder lineBuilder = new StringBuilder();

		String line;
		while ((line = inReader.readLine()) != null) {
			lineBuilder.append(line);
		}

		return new JSONObject(lineBuilder.toString());
	} catch (IOException | JSONException e) {
		String em = e.getMessage();
		Log.i(LOG_TAG, "The hook details could not be downloaded: " + (em == null ? "Unknown error" : em));
		return null;
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException ignored) {
				// no-op
			}
		}
	}
}
 
Example 18
Source File: PerfectoMobileDataProvider.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
public List<Device> readData( String xFID )
{
    List<Device> deviceList = new ArrayList<Device>( 10 );
	try
	{
		URL deviceURL = new URL( "https://" + CloudRegistry.instance(xFID).getCloud().getHostName() + "/services/handsets?operation=list&user=" + CloudRegistry.instance(xFID).getCloud().getUserName() + "&password=" + CloudRegistry.instance(xFID).getCloud().getPassword() );
		
		if ( log.isInfoEnabled() )
			log.info( "Reading Devices from " + deviceURL.toString() );
		
		URLConnection urlConnection = deviceURL.openConnection();
		urlConnection.setDoInput( true );
		
		InputStream inputStream = urlConnection.getInputStream();
			
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse( inputStream );
		
		NodeList handSets = doc.getElementsByTagName( "handset" );
		
		if ( log.isInfoEnabled() )
			log.info( "Analysing handsets using [" + pmValidator.getClass().getSimpleName() + "]" );
		
		boolean deviceFound = false;
		
		for ( int i=0; i<handSets.getLength(); i++ )
		{
			
			if ( pmValidator.validate( handSets.item(  i  ) ) )
			{
				String driverName = "";
				switch( driverType )
				{
					case APPIUM:
						String osType = getValue( handSets.item(  i  ), "os" );
						if ( osType.equals( "iOS" ) )
							driverName = "IOS";
						else if ( osType.equals( "Android" ) )
							driverName = "ANDROID";
						else
							throw new IllegalArgumentException( "Appium is not supported on the following OS " + osType );
						break;
						
					case PERFECTO:
						driverName = "PERFECTO";
						break;
						
					case WEB:
						driverName = "WEB";
						break;
				}
				
				deviceFound = true;
				deviceList.add( new SimpleDevice( getValue( handSets.item(  i  ), "deviceId" ), getValue( handSets.item(  i  ), "manufacturer" ), getValue( handSets.item(  i  ), "model" ), getValue( handSets.item(  i  ), "os" ), getValue( handSets.item(  i  ), "osVersion" ), null, null, 1, driverName, true, getValue( handSets.item(  i  ), "deviceId" ) ) );
			}
			
		}
		
		if ( !deviceFound )
			log.warn( pmValidator.getMessage() );
		
		inputStream.close();
		return deviceList;
		
		
	}
	catch( Exception e )
	{
		e.printStackTrace( );
		return null;
	}
}
 
Example 19
Source File: WatsonTranslateBox.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
private static String doUrl(URL url, String authUser, String authPassword, String data, String ctype, String method) throws IOException {
    StringBuilder sb = new StringBuilder(5000);
    try {
        if(url != null) {
            String userPassword = authUser + ":" + authPassword;
            String encodedUserPassword = Base64.encodeBytes(userPassword.getBytes());
            URLConnection con = (HttpURLConnection) url.openConnection();
            Wandora.initUrlConnection(con);
            con.setRequestProperty ("Authorization", "Basic " + encodedUserPassword);

            con.setDoInput(true);
            con.setUseCaches(false);

            if(method != null && con instanceof HttpURLConnection) {
                ((HttpURLConnection) con).setRequestMethod(method);
                //System.out.println("****** Setting HTTP request method to "+method);
            }

            if(ctype != null) {
                con.setRequestProperty("Content-type", ctype);
            }

            if(data != null && data.length() > 0) {
                con.setRequestProperty("Content-length", data.length() + "");
                con.setDoOutput(true);
                PrintWriter out = new PrintWriter(con.getOutputStream());
                out.print(data);
                out.flush();
                out.close();
            }
            //DataInputStream in = new DataInputStream(con.getInputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));

            String s;
            while ((s = in.readLine()) != null) {
                sb.append(s);
            }
            in.close();
        }
    }
    catch(IOException ioe) {
        Wandora.getWandora().handleError(ioe);
    }
    catch(Exception e) {
        Wandora.getWandora().handleError(e);
    }
    return sb.toString();
}
 
Example 20
Source File: Common.java    From samples with MIT License 4 votes vote down vote up
public static void uploadFileToS3(String filePath, String presignedUrl) {
    try {
        URLConnection urlconnection = null;
        File appFile = new File(filePath);
        URL url = new URL(presignedUrl);

        urlconnection = url.openConnection();
        urlconnection.setUseCaches(false);
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            ((HttpURLConnection) urlconnection).setRequestMethod("PUT");
            ((HttpURLConnection) urlconnection).setRequestProperty("Content-Type", "application/octet-stream");
            ((HttpURLConnection) urlconnection).setRequestProperty("x-amz-tagging", "unsaved=true");
            ((HttpURLConnection) urlconnection).setRequestProperty("Content-Length", getFileSizeBytes(appFile));
            ((HttpURLConnection) urlconnection).connect();
        }

        BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
        FileInputStream bis = new FileInputStream(appFile);
        System.out.println("Total file size to read (in bytes) : " + bis.available());
        int i;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.close();
        bis.close();

        InputStream inputStream;
        int responseCode = ((HttpURLConnection) urlconnection).getResponseCode();
        if ((responseCode >= 200) && (responseCode <= 202)) {
            inputStream = ((HttpURLConnection) urlconnection).getInputStream();
            int j;
            while ((j = inputStream.read()) > 0) {
                System.out.println(j);
            }

        } else {
            inputStream = ((HttpURLConnection) urlconnection).getErrorStream();
        }
        ((HttpURLConnection) urlconnection).disconnect();
        System.out.println("uploadFileToS3: " + ((HttpURLConnection) urlconnection).getResponseMessage());

    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}