Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. /**
  2. * Returns a new connection to the resource referred to by this URL.
  3. *
  4. * @throws IOException if an error occurs while opening the connection.
  5. */
  6. public URLConnection openConnection() throws IOException {
  7. return streamHandler.openConnection(this);
  8. }
  9.  
  10. package com.squareup.okhttp;
  11.  
  12. import java.io.IOException;
  13. import java.net.Proxy;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. import java.net.URLStreamHandler;
  17.  
  18. public class HttpHandler extends URLStreamHandler {
  19. @Override protected URLConnection openConnection(URL url) throws IOException {
  20. return newOkHttpClient(null /* proxy */).open(url);
  21. }
  22.  
  23. @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
  24. if (url == null || proxy == null) {
  25. throw new IllegalArgumentException("url == null || proxy == null");
  26. }
  27. return newOkHttpClient(proxy).open(url);
  28. }
  29.  
  30. @Override protected int getDefaultPort() {
  31. return 80;
  32. }
  33.  
  34. protected OkHttpClient newOkHttpClient(Proxy proxy) {
  35. OkHttpClient client = new OkHttpClient();
  36. client.setFollowProtocolRedirects(false);
  37. if (proxy != null) {
  38. client.setProxy(proxy);
  39. }
  40.  
  41. return client;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement