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

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

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

URLConnection.addRequestProperty介绍

[英]Adds the given property to the request header. Existing properties with the same name will not be overwritten by this method.
[中]将给定属性添加到请求标头。此方法不会覆盖同名的现有属性。

代码示例

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

  1. // Gather all cookies on the first request.
  2. URLConnection connection = new URL(url).openConnection();
  3. List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
  4. // ...
  5. // Then use the same cookies on all subsequent requests.
  6. connection = new URL(url).openConnection();
  7. for (String cookie : cookies) {
  8. connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
  9. }
  10. // ...

代码示例来源:origin: apache/incubator-druid

  1. @Override
  2. protected InputStream openObjectStream(URI object, long start) throws IOException
  3. {
  4. if (supportContentRange) {
  5. final URLConnection connection = object.toURL().openConnection();
  6. // Set header for range request.
  7. // Since we need to set only the start offset, the header is "bytes=<range-start>-".
  8. // See https://tools.ietf.org/html/rfc7233#section-2.1
  9. connection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", start));
  10. return connection.getInputStream();
  11. } else {
  12. log.warn(
  13. "Since the input source doesn't support range requests, the object input stream is opened from the start and "
  14. + "then skipped. This may make the ingestion speed slower. Consider enabling prefetch if you see this message"
  15. + " a lot."
  16. );
  17. final InputStream in = openObjectStream(object);
  18. in.skip(start);
  19. return in;
  20. }
  21. }

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

  1. URL url = new URL(urlString);
  2. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  3. conn.setReadTimeout(10000);
  4. conn.setConnectTimeout(15000);
  5. conn.addRequestProperty("Content-length", reqEntity.getContentLength()+"");
  6. conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
  7. return readStream(conn.getInputStream());

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

  1. OkHttpClient client = new OkHttpClient();
  2. OutputStream out = null;
  3. try {
  4. URL url = new URL("http://www.example.com");
  5. HttpURLConnection connection = client.open(url);
  6. for (Map.Entry<String, String> entry : multipart.getHeaders().entrySet()) {
  7. connection.addRequestProperty(entry.getKey(), entry.getValue());
  8. }
  9. connection.setRequestMethod("POST");
  10. // Write the request.
  11. out = connection.getOutputStream();
  12. multipart.writeBodyTo(out);
  13. out.close();
  14. // Read the response.
  15. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
  16. throw new IOException("Unexpected HTTP response: "
  17. + connection.getResponseCode() + " " + connection.getResponseMessage());
  18. }
  19. } finally {
  20. // Clean up.
  21. try {
  22. if (out != null) out.close();
  23. } catch (Exception e) {
  24. }
  25. }

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

  1. private URLConnection openConnection(URL finalURL) throws IOException {
  2. URLConnection connection = finalURL.openConnection();
  3. final boolean http = connection instanceof HttpURLConnection;
  4. if (http && tryGzip) {
  5. connection.addRequestProperty("Accept-Encoding", "gzip");
  6. }
  7. // mind, connect timeout is in seconds
  8. if (http && getConnectTimeout() > 0) {
  9. connection.setConnectTimeout(1000 * getConnectTimeout());
  10. }
  11. if (http && getReadTimeout() > 0) {
  12. connection.setReadTimeout(1000 * getReadTimeout());
  13. }
  14. final String username = getUser();
  15. final String password = getPassword();
  16. if (http && username != null && password != null) {
  17. String userpassword = username + ":" + password;
  18. String encodedAuthorization =
  19. Base64.encodeBytes(userpassword.getBytes("UTF-8"), Base64.DONT_BREAK_LINES);
  20. connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
  21. }
  22. return connection;
  23. }

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

  1. private InputStream read() {
  2. try {
  3. HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
  4. httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
  5. return httpcon.getInputStream();
  6. } catch (IOException e) {
  7. String error = e.toString();
  8. throw new RuntimeException(e);
  9. }
  10. }

代码示例来源:origin: org.apache.xmlbeans/xmlbeans

  1. conn = url.openConnection();
  2. conn.addRequestProperty("User-Agent", USER_AGENT);
  3. conn.addRequestProperty("Accept", "application/xml, text/xml, */*");
  4. if (conn instanceof HttpURLConnection)
  5. else
  6. url = new URL(newLocation);
  7. count ++;
  8. stream = conn.getInputStream();
  9. return parse( stream, type, options );

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

  1. String addr = "http://172.26.41.18:8080/domain/list";
  2. URL url = new URL(addr);
  3. HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
  4. httpCon.setUseCaches(false);
  5. httpCon.setAllowUserInteraction(false);
  6. httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
  7. System.out.println(httpCon.getResponseCode());
  8. System.out.println(httpCon.getResponseMessage());

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

  1. final URLConnection connection = url.openConnection();
  2. connection.addRequestProperty("Accept", accept != null ? accept : parseAsText ? "text/plain,text/*" : "application/xml,text/xml,application/*+xml,text/*+xml");
  3. if (acceptLanguage != null) connection.addRequestProperty("Accept-Language", acceptLanguage);
  4. inputStream = connection.getInputStream();
  5. try {
  6. if (parseAsText) {

代码示例来源:origin: rhuss/jolokia

  1. @Override
  2. public Result authenticate(HttpExchange pHttpExchange) {
  3. try {
  4. URLConnection connection = delegateURL.openConnection();
  5. connection.addRequestProperty("Authorization",
  6. pHttpExchange.getRequestHeaders().getFirst("Authorization"));
  7. connection.setConnectTimeout(2000);
  8. connection.connect();
  9. if (connection instanceof HttpURLConnection) {
  10. HttpURLConnection httpConnection = (HttpURLConnection) connection;
  11. return httpConnection.getResponseCode() == 200 ?
  12. new Success(principalExtractor.extract(connection)) :
  13. new Failure(401);
  14. } else {
  15. return new Failure(401);
  16. }
  17. } catch (final IOException e) {
  18. return prepareFailure(pHttpExchange, "Cannot call delegate url " + delegateURL + ": " + e, 503);
  19. } catch (final IllegalArgumentException e) {
  20. return prepareFailure(pHttpExchange, "Illegal Argument: " + e, 400);
  21. } catch (ParseException e) {
  22. return prepareFailure(pHttpExchange, "Invalid JSON response: " + e, 422);
  23. }
  24. }

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

  1. try {
  2. HttpURLConnection connection = ((HttpURLConnection)url.openConnection());
  3. connection.addRequestProperty("User-Agent", "Mozilla/4.0");
  4. InputStream input;
  5. if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works
  6. input = connection.getInputStream();
  7. else input = connection.getErrorStream();
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  9. String msg;
  10. while ((msg =reader.readLine()) != null)
  11. System.out.println(msg);
  12. } catch (IOException e) {
  13. System.err.println(e);
  14. }

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

  1. try {
  2. url = new URL("https://www.google.com/accounts/ClientLogin");
  3. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  4. urlConnection.setRequestMethod("POST");
  5. urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  6. url = new URL(googleReaderTokenUrl);
  7. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  8. Log.d("GoogleReader", "Auth.b=" + authorization);
  9. urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization);
  10. urlConnection.setRequestMethod("GET");
  11. urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
  12. InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
  13. responseString = convertStreamToString(inputStream);
  14. }catch(Exception e){
  15. url = new URL(googleAuthUrl);
  16. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  17. urlConnection.setRequestMethod("POST");
  18. Log.d("GoogleReader", "Auth.c=" + authorization);
  19. urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization);
  20. urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  21. urlConnection.setRequestProperty("Content-Length", Integer.toString(sb.toString().getBytes("UTF-8").length));

代码示例来源:origin: MindorksOpenSource/PRDownloader

  1. @Override
  2. public void connect(DownloadRequest request) throws IOException {
  3. connection = new URL(request.getUrl()).openConnection();
  4. connection.setReadTimeout(request.getReadTimeout());
  5. connection.setConnectTimeout(request.getConnectTimeout());
  6. final String range = String.format(Locale.ENGLISH,
  7. "bytes=%d-", request.getDownloadedBytes());
  8. connection.addRequestProperty(Constants.RANGE, range);
  9. connection.addRequestProperty(Constants.USER_AGENT, request.getUserAgent());
  10. addHeaders(request);
  11. connection.connect();
  12. }

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

  1. public static String getHTML(URL url) {
  2. try {
  3. final URLConnection urlConnection = url.openConnection();
  4. urlConnection.addRequestProperty("User-Agent", "Foo?");
  5. final InputStream inputStream = urlConnection.getInputStream();
  6. final String html = IOUtils.toString(inputStream);
  7. inputStream.close();
  8. return html;
  9. } catch (Exception e) {
  10. throw new RuntimeException(e);
  11. }

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

  1. URI uri = new URI("the server address goes here");
  2. HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
  3. conn.setDoOutput(true);
  4. conn.setRequestMethod("PUT");
  5. conn.addRequestProperty("Content-Type", "application/json");
  6. OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
  7. out.write(gson.toJson(newClient));
  8. out.close();
  9. // Check here that you succeeded!

代码示例来源:origin: org.apache.xmlbeans/xmlbeans

  1. URL url = new URL( schemaLocation );
  2. URLConnection conn = url.openConnection();
  3. conn.addRequestProperty("User-Agent", USER_AGENT);
  4. conn.addRequestProperty("Accept", "application/xml, text/xml, */*");
  5. DigestInputStream input = digestInputStream(conn.getInputStream());
  6. IOUtil.copyCompletely(input, buffer);
  7. digest = HexBin.bytesToString(input.getMessageDigest().digest());

代码示例来源:origin: org.apache.ant/ant

  1. private URLConnection openConnection(final URL aSource) throws IOException {
  2. final URLConnection connection = aSource.openConnection();
  3. connection.addRequestProperty("User-Agent", this.userAgent);
  4. : "") + " moved to " + newLocation;
  5. log(message, logLevel);
  6. final URL newURL = new URL(aSource, newLocation);
  7. if (!redirectionAllowed(aSource, newURL)) {
  8. return null;

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

  1. private static InputStream openGZIPInputStream(URL url, String auth, int timeout) throws IOException {
  2. final URLConnection connection = url.openConnection();
  3. connection.setConnectTimeout(CONNECTION_TIMEOUT);
  4. connection.setReadTimeout(timeout);
  5. connection.addRequestProperty(GZIP_HTTP_HEADER, GZIP_COMPRESSION);
  6. if (null != auth) {
  7. connection.setRequestProperty("Authorization", auth);
  8. }
  9. return new GZIPInputStream(connection.getInputStream());
  10. }

代码示例来源:origin: owlcs/owlapi

  1. protected static URLConnection rebuildConnection(OWLOntologyLoaderConfiguration config,
  2. int connectionTimeout, URL newURL, String acceptHeaders) throws IOException {
  3. URLConnection conn;
  4. conn = newURL.openConnection();
  5. conn.addRequestProperty("Accept", acceptHeaders);
  6. if (config.isAcceptingHTTPCompression()) {
  7. conn.setRequestProperty("Accept-Encoding", ACCEPTABLE_CONTENT_ENCODING);
  8. }
  9. conn.setConnectTimeout(connectionTimeout);
  10. return conn;
  11. }

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

  1. public static void main(String[] args) throws Exception {
  2. URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
  3. URLConnection connection = whatismyip.openConnection();
  4. connection.addRequestProperty("Protocol", "Http/1.1");
  5. connection.addRequestProperty("Connection", "keep-alive");
  6. connection.addRequestProperty("Keep-Alive", "1000");
  7. connection.addRequestProperty("User-Agent", "Web-Agent");
  8. BufferedReader in =
  9. new BufferedReader(new InputStreamReader(connection.getInputStream()));
  10. String ip = in.readLine(); //you get the IP as a String
  11. System.out.println(ip);
  12. }

相关文章

URLConnection类方法