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

The following examples show how to use java.net.URLConnection#setDoInput() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: AthenaServing   File: SslTest.java    License: Apache License 2.0 6 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 Project: dawnsci   File: SliceClient.java    License: 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 3
Source Project: servicemix   File: Client.java    License: 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 4
Source Project: wx-crawl   File: HttpRequestUtils.java    License: 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 5
Source Project: keycloak   File: CertificateValidator.java    License: Apache License 2.0 6 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 6
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 7
Source Project: micro-integrator   File: FileServiceApp.java    License: 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 8
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 9
/**
 * 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 10
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 11
/**
 * 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 12
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
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 Project: lightstep-tracer-java   File: BenchmarkClient.java    License: 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
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 Project: SENS   File: SensUtils.java    License: 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
@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
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
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 Project: samples   File: Common.java    License: 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());
    }
}