java.net.URLConnection.connect()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(291)

本文整理了Java中java.net.URLConnection.connect()方法的一些代码示例,展示了URLConnection.connect()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.connect()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:connect

URLConnection.connect介绍

[英]Opens a connection to the resource. This method will not reconnect to a resource after the initial connection has been closed.
[中]打开与资源的连接。关闭初始连接后,此方法不会重新连接到资源。

代码示例

代码示例来源:origin: stackoverflow.com

URL url = new URL(user_image_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
conn.setDoInput(true);   
conn.connect();     
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);

代码示例来源:origin: redisson/redisson

private static URLConnection fetchClass0(String host, int port,
                       String filename)
    throws IOException
  {
    URL url;
    try {
      url = new URL("http", host, port, filename);
    }
    catch (MalformedURLException e) {
      // should never reache here.
      throw new IOException("invalid URL?");
    }

    URLConnection con = url.openConnection();
    con.connect();
    return con;
  }
}

代码示例来源:origin: code4craft/tiny-spring

@Override
  public InputStream getInputStream() throws IOException{
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();
    return urlConnection.getInputStream();
  }
}

代码示例来源:origin: jenkinsci/jenkins

@RequirePOST
public FormValidation doCheckUrl(@QueryParameter String value) {
  Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
  
  try {
    URLConnection conn = ProxyConfiguration.open(new URL(value));
    conn.connect();
    if (conn instanceof HttpURLConnection) {
      if (((HttpURLConnection) conn).getResponseCode() != HttpURLConnection.HTTP_OK) {
        return FormValidation.error(Messages.ZipExtractionInstaller_bad_connection());
      }
    }
    return FormValidation.ok();
  } catch (MalformedURLException x) {
    return FormValidation.error(Messages.ZipExtractionInstaller_malformed_url());
  } catch (IOException x) {
    return FormValidation.error(x,Messages.ZipExtractionInstaller_could_not_connect());
  }
}

代码示例来源:origin: javax.xml.bind/jaxb-api

URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(false);
con.connect();
return new StreamResult(con.getOutputStream());

代码示例来源:origin: stackoverflow.com

System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false); 
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout(30000);
conn.setDoOutput(true); 
conn.setDoInput(true); 
consumer.sign(conn);

conn.connect();

InputSource is = new InputSource(conn.getInputStream());

代码示例来源:origin: stackoverflow.com

URLConnection conn = url.openConnection();
conn.connect();

inputStream = conn.getInputStream();

bufferedInputStream = new BufferedInputStream(inputStream);

bmp = BitmapFactory.decodeStream(bufferedInputStream);

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
  "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://server.com/file.mp3");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();

代码示例来源:origin: apache/hbase

private String getUrlContent(URL u) throws IOException {
  java.net.URLConnection c = u.openConnection();
  c.setConnectTimeout(2000);
  c.setReadTimeout(2000);
  c.connect();
  try (InputStream in = c.getInputStream()) {
   return IOUtils.toString(in);
  }
 }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

代码示例来源:origin: stackoverflow.com

try {
  URL url = new URL("http://www.yoursite.com/");
  URLConnection conn = url.openConnection();
  conn.connect();
} catch (MalformedURLException e) {
  // the URL is not in a valid form
} catch (IOException e) {
  // the connection couldn't be established
}

代码示例来源:origin: stackoverflow.com

public static Bitmap getBitmapFromURL(String src) {
  try {
    URL url = new URL(src);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
  } catch (IOException e) {
    // Log exception
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

new URL("https://letsencrypt.org/").openConnection().connect();

代码示例来源:origin: stackoverflow.com

HttpURLConnection connection = null;
try {
  URL url = new URL(sUrl[0]);
  connection = (HttpURLConnection) url.openConnection();
  connection.connect();
  input = connection.getInputStream();
  output = new FileOutputStream("/sdcard/file_name.extension");
      input.close();
      return null;
      output.close();
    if (input != null)
      input.close();
  } catch (IOException ignored) {

代码示例来源:origin: stackoverflow.com

public static boolean hasActiveInternetConnection(Context context) {
  if (isNetworkAvailable(context)) {
    try {
      HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
      urlc.setRequestProperty("User-Agent", "Test");
      urlc.setRequestProperty("Connection", "close");
      urlc.setConnectTimeout(1500); 
      urlc.connect();
      return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
      Log.e(LOG_TAG, "Error checking internet connection", e);
    }
  } else {
    Log.d(LOG_TAG, "No network available!");
  }
  return false;
}

代码示例来源:origin: stackoverflow.com

URLConnection con = new URL( url ).openConnection();
System.out.println( "orignal url: " + con.getURL() );
con.connect();
System.out.println( "connected url: " + con.getURL() );
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
is.close();

代码示例来源:origin: stackoverflow.com

HttpURLConnection con = (HttpURLConnection)(new URL( url ).openConnection());
con.setInstanceFollowRedirects( false );
con.connect();
int responseCode = con.getResponseCode();
System.out.println( responseCode );
String location = con.getHeaderField( "Location" );
System.out.println( location );

代码示例来源:origin: stackoverflow.com

ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
try {
  URL url = new URL(urlToDownload);
  URLConnection connection = url.openConnection();
  connection.connect();
  InputStream input = new BufferedInputStream(connection.getInputStream());
  OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk");
  input.close();
} catch (IOException e) {
  e.printStackTrace();

代码示例来源:origin: stackoverflow.com

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();

相关文章

URLConnection类方法