URLConnection setRequestProperty - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for URLConnection setRequestProperty method.

This code example shows how to use the following methods:setRequestMethod, setRequestProperty, setSSLSocketFactory
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, trustManagerFactory.getTrustManagers(), null);
 
      URL url = new URL(String.format("https://gcm.textsecure.whispersystems.org%s", urlFragment));
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setSSLSocketFactory(context.getSocketFactory());
      connection.setRequestMethod(method);
      connection.setRequestProperty("Content-Type", "application/json");
 
      if (password != null) {
        System.out.println("Adding authorization header: " + getAuthorizationHeader());
        connection.setRequestProperty("Authorization", getAuthorizationHeader());
      } 
 
      return connection;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    } catch (KeyManagementException e) {
      throw new AssertionError(e);
    } catch (MalformedURLException e) {
8
CodeRank
This code example shows how to use the following methods:setRequestMethod, setDoInput, setDoOutput, setRequestProperty, setUseCaches
		} 
		return new Request.Builder().source("com.apple.maps").unknown3(0).unknown4(0).wifis(wifis).build(); 
	} 
 
	private static void prepareConnection(HttpsURLConnection connection, int length) throws ProtocolException {
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_TYPE, CONTENT_TYPE_URLENCODED);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_LENGTH, String.valueOf(length));
	} 
 
	public Response retrieveLocations(String... macs) throws IOException {
		Request request = createRequest(macs);
		byte[] byteb = request.toByteArray();
		byte[] bytes = combineBytes(APPLE_MAGIC_BYTES, byteb, (byte) byteb.length);
		HttpsURLConnection connection = createConnection();
		prepareConnection(connection, bytes.length);
		OutputStream out = connection.getOutputStream();
7
CodeRank
This code example shows how to use the following methods:disconnect, getResponseCode, connect, setRequestProperty
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection)) {
      throw new IOException();
    } 
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setRequestProperty("Accept", contentTypes);
    connection.setRequestProperty("Accept-Charset", "utf-8,*");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try { 
      try { 
        connection.connect();
      } catch (NullPointerException npe) {
        // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(npe);
      } catch (IllegalArgumentException iae) {
        // Also seen this in the wild, not sure what to make of it. Probably a bad URL 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(iae);
      } 

10
CodeRank
This code example shows how to use the following methods:setRequestMethod, getOutputStream, setDoOutput, setRequestProperty
    } 
 
    public static URLConnection httpPost(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
        urlConnection.getOutputStream().write(body.getBytes(Charset.forName("UTF8")));
        return urlConnection;
    } 
 
    public static URLConnection httpPostCompressed(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.addRequestProperty("Content-Encoding", "gzip");
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
 
8
CodeRank
This code example shows how to use the following methods:setRequestMethod, setConnectTimeout, setDoInput, setDoOutput, setReadTimeout
    } 
 
    private HttpURLConnection openConnection(URL targetUrl) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
        if (0 < getTimeoutMillis()) { 
            conn.setConnectTimeout(getTimeoutMillis());
            conn.setReadTimeout(getTimeoutMillis());
        } 
        if (isTryGzip()) { 
            conn.setRequestProperty("Accept-Encoding", "gzip");
        } 
 
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        return conn;
    } 
} 
7
CodeRank
This code example shows how to use the following methods:addRequestProperty, connect, setConnectTimeout, setReadTimeout, setRequestProperty
        URLConnection connection = openConnection(url);
        if(connection instanceof HttpURLConnection){
            ((HttpURLConnection) connection).setRequestMethod("GET");
        } 
 
        connection.connect();
 
        return new SimpleHTTPResponse(connection);
    } 
 
    /** 
     * @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream, 
     *      java.lang.String) 
     */ 
    public HTTPResponse post(final URL url, final InputStream postContent,
            final String postContentType) throws IOException {
 
        URLConnection connection = openConnection(url);
        if (connection instanceof HttpURLConnection) {
            ((HttpURLConnection) connection).setRequestMethod("POST");

7
CodeRank
This code example shows how to use the following methods:setRequestMethod, setRequestProperty
	 * @param request 
	 * @return configured request 
	 */ 
	protected HttpURLConnection configureRequest(final HttpURLConnection request) {
		if (credentials != null)
			request.setRequestProperty(HEADER_AUTHORIZATION, credentials);
		request.setRequestProperty(HEADER_USER_AGENT, userAgent);
		request.setRequestProperty(HEADER_ACCEPT,
				"application/vnd.github.beta+json"); //$NON-NLS-1$ 
		return request;
	} 
 
	/** 
	 * Configure request URI 
	 * 
	 * @param uri 
	 * @return configured URI 
	 */ 
	protected String configureUri(final String uri) {
		if (prefix == null || uri.startsWith(prefix))

7
CodeRank
This code example shows how to use the following methods:setRequestMethod, getInputStream, setRequestProperty
	public static String useragent;
 
	/** mainly sets the User-Agent we need */ 
	private static void setUserAgent(HttpURLConnection conn) {
		if (useragent != null)
			conn.setRequestProperty("User-Agent", useragent);
	} 
 
	public static URL getRedirectedUrl(URL url) throws IOException {
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		setUserAgent(conn);
		conn.setRequestMethod("GET");
		conn.setInstanceFollowRedirects(false);
		int rc = conn.getResponseCode();
		if (rc != REDIRECT_RESPONSE_CODE) {
			throw new IOException("code " + rc + " '" + conn.getResponseMessage() + "'");
		} 
		String location = conn.getHeaderField("Location");
		if (location == null) {
			throw new IOException("No 'Location' header found");

7
CodeRank
This code example shows how to use the following methods:setRequestMethod, setDoInput, setDoOutput, setRequestProperty
        } 
    } 
 
    private HttpURLConnection getHttpURLConnection(final String url, final String contentType) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod(POST_REQUEST_METHOD);
        connection.setRequestProperty("Content-Type", contentType);
        return connection;
    } 
 
    private void writeUploadRequest(final InputStream is, final OutputStream os) throws IOException {
        os.write(buildUploadPostRequestHeader());
        writePostRequestPayload(is, os);
        os.write((CRLF + BOUNDARY + "--" + CRLF).getBytes("US-ASCII"));
        os.flush();
    } 
 
    private void writeAddRequest(BufferedOutputStream os, byte[] hash) throws IOException {
8
CodeRank
This code example shows how to use the following methods:setInstanceFollowRedirects, getHeaderField, setConnectTimeout, setReadTimeout, setRequestProperty
 
	private static SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
 
	private static HttpURLConnection connectToHttpURL(String url, int followRedirects) throws Exception, MalformedURLException {
		HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
		connection.setRequestProperty("User-Agent", "http://jadn.com/carcast");
		connection.setConnectTimeout(30 * 1000);
		connection.setReadTimeout(20 * 1000);
		// Android seems to handle redirects improperly: 
		// the InputStream is of the redirect itself, not the redirected page 
		connection.setInstanceFollowRedirects(false);
 
		// TODO: is there a better way to properly follow redirects? 
		String redirectLocation = connection.getHeaderField("Location");
		if (redirectLocation == null || "".equals(redirectLocation)) {
			return connection;
		} 
 
		if (followRedirects == 0) {
			throw new Exception("Maximum HTTP redirects reached");
7
CodeRank

Related URLConnection setRequestProperty Questions & Answers:

Other URLConnection setRequestProperty Examples

100:	                    e.getMessage());
101:	            CommonUtilities.displayMessage(context, message);
102:	        }
103:	    }
104:

Read More

setRequestProperty Method Overview
Sets the general request property.
See Code Examples for other Java 8 URLConnection Methods: