java.lang.Object
java.net.URLConnection
java.net.HttpURLConnection
- Direct Known Subclasses:
- HttpsURLConnection
- See Also:
- Top Examples, Source Code,
disconnect()
protected int chunkLength
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void disconnect()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[70]Http PUT
By Anonymous on 2005/08/09 20:13:39 Rate
try {
URL u = new URL ( "http://www.KickJava.com/" ) ;
HttpURLConnection huc = ( HttpURLConnection ) u.openConnection ( ) ;
huc.setRequestMethod ( "PUT" ) ;
huc.connect ( ) ;
OutputStream os = huc.getOutputStream ( ) ;
int code = huc.getResponseCode ( ) ;
if ( code > = 200 && < 300 ) {
// put the data...
}
huc.disconnect ( ) ;
}
catch ( IOException e ) { //...
protected int fixedContentLength
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1745]Downloading files with HttpURLConnection
By Jos?? Antonio ??lvarez Ruiz on 2006/04/22 20:34:07 Rate
import java.net.*;
import java.io.*;
public class DownloadTest {
DownloadTest ( String dir ) {
dir = dir.replace ( " ","%20" ) ;
try {
URL u = new URL ( dir ) ;
HttpURLConnection huc = ( HttpURLConnection ) u.openConnection ( ) ;
huc.setRequestMethod ( "GET" ) ;
huc.connect ( ) ;
dir = dir.replaceAll ( "%20","" ) ;
String [ ] tokens = dir.split ( "/" ) ;
String fileName = tokens [ tokens.length -1 ] ;
InputStream is = huc.getInputStream ( ) ;
int code = huc.getResponseCode ( ) ;
if ( code == HttpURLConnection.HTTP_OK ) {
byte [ ] buffer = new byte [ 4096 ] ;
File output = new File ( fileName ) ;
FileOutputStream outputStream= new FileOutputStream ( output ) ;
System.out.println ( "Ready to download " + fileName +" " + huc.getContentLength ( ) + " bytes" ) ;
int totBytes,bytes,sumBytes = 0;
totBytes = huc.getContentLength ( ) ;
while ( true ) {
bytes = is.read ( buffer ) ;
if ( bytes < = 0 ) break;
sumBytes+= bytes;
outputStream.write ( buffer,0,bytes ) ;
System.out.println ( sumBytes + " of " + totBytes + " " + ( ( float ) sumBytes/ ( float ) totBytes ) *100 + "% done" ) ;
}
outputStream.close ( ) ;
}
huc.disconnect ( ) ;
}
catch ( IOException e ) {
System.out.println ( "Exception\n" + e ) ;
}
}
public static void main ( String args [ ] ) {
if ( args.length != 1 ) {
System.out.println ( "You have to provide an URL as argument" ) ;
System.exit ( 1 ) ;
}
DownloadTest t = new DownloadTest ( args [ 0 ] ) ;
}
}
public InputStream getErrorStream()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static boolean getFollowRedirects()
- See Also:
setFollowRedirects(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getHeaderField(int n)
- See Also:
getHeaderFieldKey(int)
, URLConnection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public long getHeaderFieldDate(String name,
long Default)
- See Also:
- URLConnection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getHeaderFieldKey(int n)
- See Also:
- URLConnection,
getHeaderField(0)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getInstanceFollowRedirects()
- See Also:
setInstanceFollowRedirects(boolean)
, instanceFollowRedirects
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Permission getPermission()
throws IOException
- See Also:
- URLConnection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getRequestMethod()
- See Also:
setRequestMethod(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getResponseCode()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1563]Http connect via proxy
By Anonymous on 2005/10/07 10:36:52 Rate
String url = "http://www.kickjava.com/",
proxy = "myProxy",
port = "8080";
URL server = new URL ( url ) ;
Properties systemProperties = System.getProperties ( ) ;
systemProperties.setProperty ( "http.proxyHost",proxy ) ;
systemProperties.setProperty ( "http.proxyPort",port ) ;
HttpURLConnection connection = ( HttpURLConnection ) server.openConnection ( ) ;
if ( HttpURLConnection.HTTP_OK != connection.getResponseCode ( ) ) {
//do something ...
}
public String getResponseMessage()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected HttpURLConnection(URL u)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_ACCEPTED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_BAD_GATEWAY
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_BAD_METHOD
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_BAD_REQUEST
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_CLIENT_TIMEOUT
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_CONFLICT
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_CREATED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_ENTITY_TOO_LARGE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_FORBIDDEN
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_GATEWAY_TIMEOUT
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_GONE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_INTERNAL_ERROR
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_LENGTH_REQUIRED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_MOVED_PERM
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_MOVED_TEMP
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_MULT_CHOICE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NOT_ACCEPTABLE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NOT_AUTHORITATIVE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NOT_FOUND
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NOT_IMPLEMENTED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NOT_MODIFIED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_NO_CONTENT
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_OK
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[510]Check if file (by http/url) exists
By Anonymous on 2004/01/21 07:42:45 Rate
// check if file ( by http/url ) exists pb20031114
static boolean fileExists ( String name )
{
boolean fileIsThere = false;
java.net.HttpURLConnection huc = null;
try
{
java.net.URL myURI = new java.net.URL ( name ) ;
huc = ( java.net.HttpURLConnection ) myURI.openConnection ( ) ;
huc.setRequestMethod ( "HEAD" ) ;
huc.connect ( ) ;
if ( huc.getResponseCode ( ) ==java.net.HttpURLConnection.HTTP_OK )
fileIsThere = true;
}
catch ( Exception e )
{
}
finally
{
try { huc.disconnect ( ) ; } catch ( Exception e ) { }
}
return fileIsThere;
}
[2007]Correction
By mej1960 { at } yahoo { dot } com on 2010/02/15 10:26:50 Rate
The given sample code allegedly showing if the file exists is incorrect.
For example, running the code with the URL "http://wap.yahoo.com"
claims the file does not exist. But it does exist. For whatever reason,
Yahoo! returns not HTTP_OK=200 but 500 ( internal server ) error
for this URL if you try to execute the HEAD method.
public static final int HTTP_PARTIAL
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_PAYMENT_REQUIRED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_PRECON_FAILED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_PROXY_AUTH
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_REQ_TOO_LONG
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_RESET
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_SEE_OTHER
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
@Deprecated
public static final int HTTP_SERVER_ERROR
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_UNAUTHORIZED
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_UNAVAILABLE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_UNSUPPORTED_TYPE
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_USE_PROXY
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static final int HTTP_VERSION
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected boolean instanceFollowRedirects
- See Also:
setFollowRedirects(boolean)
, getInstanceFollowRedirects()
, setInstanceFollowRedirects(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected String method
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected int responseCode
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected String responseMessage
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setChunkedStreamingMode(int chunklen)
- See Also:
setFixedLengthStreamingMode(int)
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setFixedLengthStreamingMode(int contentLength)
- See Also:
setChunkedStreamingMode(int)
, IllegalArgumentException, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static void setFollowRedirects(boolean set)
- See Also:
getFollowRedirects()
, SecurityManager.checkSetFactory()
, SecurityException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setInstanceFollowRedirects(boolean followRedirects)
- See Also:
getInstanceFollowRedirects()
, instanceFollowRedirects
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setRequestMethod(String method)
throws ProtocolException
- See Also:
getRequestMethod()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean usingProxy()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples