コード例 #1
1
  @Override
  public void run() {
    if (mDownloadManager != null) {
      mDownloadManager.onStartDownload(this);
    }
    mException = null;
    InputStream inputStream = null;
    BufferedWriter bufferedWriter = null;
    try {
      int bufferSize = 8192;
      if (mDownloadRateLimit > 0 && bufferSize > mDownloadRateLimit << 10) {
        bufferSize = mDownloadRateLimit << 10;
      }
      mTargetFile = new File(getTempFilePath());
      URL url = new URL(getTargetURL());
      String host = url.getHost();
      int port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort();

      mSocket = new Socket();
      mSocket.setReceiveBufferSize(bufferSize);
      mSocket.setSoTimeout(getTimeoutInterval());
      SocketAddress address = new InetSocketAddress(host, port);
      mSocket.connect(address, getTimeoutInterval());

      Log.i(TAG, "socket receive buffer size is: " + mSocket.getReceiveBufferSize());
      bufferedWriter =
          new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream(), "UTF8"));
      String requestStr = "GET " + url.getFile() + " HTTP/1.1\r\n";

      String hostHeader = "Host: " + host + "\r\n";
      String acceptHeader =
          "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
      String charsetHeader = "Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
      String languageHeader = "Accept-Language: zh-CN,zh;q=0.8\r\n";
      String keepHeader = "Connection: close\r\n";

      bufferedWriter.write(requestStr);
      bufferedWriter.write(hostHeader);
      bufferedWriter.write(acceptHeader);
      bufferedWriter.write(charsetHeader);
      bufferedWriter.write(languageHeader);
      bufferedWriter.write(keepHeader);
      if (mLoadedByteLength > 0) {
        bufferedWriter.write("Range: bytes=" + mLoadedByteLength + "-\r\n");
      } else {
        guessFileName();
        String folderPath = getFilePath().substring(0, getFilePath().lastIndexOf("/"));
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
          folder.mkdirs();
        } else {
          deleteFile(getFilePath());
          deleteFile(getTempFilePath());
        }
      }
      bufferedWriter.write("\r\n");
      bufferedWriter.flush();
      inputStream = mSocket.getInputStream();
      Log.i(TAG, inputStream.getClass().getName());
      HttpResponseHeaderParser responseHeader = new HttpResponseHeaderParser();
      String responseHeaderLine = null;
      char readChar = 0;
      StringBuilder headerBuilder = new StringBuilder();
      while ((byte) (readChar = (char) inputStream.read()) != -1) {
        headerBuilder.append(readChar);
        if (readChar == 10) {
          responseHeaderLine = headerBuilder.substring(0, headerBuilder.length() - 2);
          headerBuilder.setLength(0);
          if (responseHeaderLine.length() == 0) {
            break;
          } else {
            responseHeader.addResponseHeaderLine(responseHeaderLine);
            Log.i(TAG, responseHeaderLine);
          }
        }
      }
      Log.i(TAG, "status code: " + responseHeader.getStatusCode());

      if (mTotalByteLength == 0) {
        mTotalByteLength = responseHeader.getContentLength();
      }
      mOutputStream = new FileOutputStream(mTargetFile, true);
      mByteOutput = new ByteArrayOutputStream();
      byte[] buffer = new byte[bufferSize];
      int length = -1;
      mTimeStart = System.currentTimeMillis();
      mDeltaLByteLength = 0;
      mSleepTime = 0;
      while ((length = inputStream.read(buffer)) != -1 && mIsDownloading) {
        Log.i(TAG, "receive data: " + length + " available: " + inputStream.available());
        mByteOutput.write(buffer, 0, length);
        if (mByteOutput.size() >= getMemoryCacheSize() << 10) {
          writeCache();
        }
        limitTheByteRate(length);
      }
      Log.i(TAG, "receive data: " + length + " available: " + inputStream.available());

    } catch (Exception e) {
      mException = e;
    } finally {
      if (mException != null && mIsDownloading && mDownloadManager != null) {
        mDownloadManager.onFailedDownload(this);
      }
      try {
        if (bufferedWriter != null) {
          bufferedWriter.close();
          bufferedWriter = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (inputStream != null) {
          inputStream.close();
          inputStream = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      stop();
      writeCache();
      try {
        if (mOutputStream != null) {
          mOutputStream.close();
          mOutputStream = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (mByteOutput != null) {
          mByteOutput.close();
          mByteOutput = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
コード例 #2
0
ファイル: Browser.java プロジェクト: Lujango/frostwire
  private String getBase(final String string) {
    if (string == null) {
      return "";
    }

    final String base = this.getRegex("<base\\s*href=\"(.*?)\"").getMatch(0);
    if (base != null) {
      return base;
    }

    final URL url = this.request.getHttpConnection().getURL();
    final String host = url.getHost();
    String portUse = "";
    if (url.getDefaultPort() > 0 && url.getPort() > 0 && url.getDefaultPort() != url.getPort()) {
      portUse = ":" + url.getPort();
    }
    String proto = "http://";
    if (url.toString().startsWith("https")) {
      proto = "https://";
    }
    String path = url.getPath();
    int id;
    if ((id = path.lastIndexOf('/')) >= 0) {
      path = path.substring(0, id);
    }
    return proto + host + portUse + path + "/";
  }
コード例 #3
0
 /**
  * Sets a custom Sumo Logic API url, i.e., different from https://api.sumologic.com.
  *
  * @param urlString The custom sumo logic api URL.
  * @throws MalformedURLException On URL syntax error.
  */
 public void setURL(String urlString) throws MalformedURLException {
   URL url = new URL(urlString);
   this.hostname = url.getHost();
   this.port =
       (url.getPort() == -1)
           ? (url.getDefaultPort() == -1 ? 80 : url.getDefaultPort())
           : url.getPort();
   this.protocol = url.getProtocol();
 }
コード例 #4
0
ファイル: BaiduAPI.java プロジェクト: jibaro/stonelion
  public static URL getURL(double latitude, double longitude, String output)
      throws MalformedURLException, IOException {
    StringBuilder stringBuilder = new StringBuilder(URL_PREFIX);
    stringBuilder.append("?");
    stringBuilder.append("output=" + output + "&");
    stringBuilder.append("location=" + latitude + "," + longitude + "&");
    stringBuilder.append("key=" + PRIVATE_KEY);

    URL url = new URL(stringBuilder.toString());

    System.out.println(String.format("getProtocol %s", url.getProtocol()));
    System.out.println(String.format("getHost %s", url.getHost()));
    System.out.println(String.format("getPath %s", url.getPath()));
    System.out.println(String.format("getPort %s", url.getPort()));
    System.out.println(String.format("getDefaultPort %s", url.getDefaultPort()));
    System.out.println(String.format("getQuery %s", url.getQuery()));
    System.out.println(String.format("getAuthority %s", url.getAuthority()));
    System.out.println(String.format("getRef %s", url.getRef()));
    System.out.println(String.format("getUserInfo %s", url.getUserInfo()));
    System.out.println(String.format("getFile %s", url.getFile()));
    System.out.println(String.format("getContent %s", url.getContent()));
    System.out.println(String.format("toExternalForm %s", url.toExternalForm()));
    System.out.println("---------------");

    return url;
  }
コード例 #5
0
ファイル: MmsConnection.java プロジェクト: Diapolo/TextSecure
  protected CloseableHttpClient constructHttpClient() throws IOException {
    RequestConfig config =
        RequestConfig.custom()
            .setConnectTimeout(20 * 1000)
            .setConnectionRequestTimeout(20 * 1000)
            .setSocketTimeout(20 * 1000)
            .setMaxRedirects(20)
            .build();

    URL mmsc = new URL(apn.getMmsc());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    if (apn.hasAuthentication()) {
      credsProvider.setCredentials(
          new AuthScope(
              mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
          new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
    }

    return HttpClients.custom()
        .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
        .setRedirectStrategy(new LaxRedirectStrategy())
        .setUserAgent("Android-Mms/2.0")
        .setConnectionManager(new BasicHttpClientConnectionManager())
        .setDefaultRequestConfig(config)
        .setDefaultCredentialsProvider(credsProvider)
        .build();
  }
コード例 #6
0
  public String getContextByPostMethod(String url, List<KeyValue> params) {
    HttpClient client = getHttpClient();

    client.getParams().setParameter("http.protocol.content-charset", this.codeing);

    PostMethod post = null;
    String result = "";
    try {
      URL u = new URL(url);
      client
          .getHostConfiguration()
          .setHost(
              u.getHost(), u.getPort() == -1 ? u.getDefaultPort() : u.getPort(), u.getProtocol());

      post = new PostMethod(u.getPath());

      NameValuePair[] nvps = new NameValuePair[params.size()];
      int i = 0;
      for (KeyValue kv : params) {
        nvps[i] = new NameValuePair(kv.getKey(), kv.getValue());
        i++;
      }

      post.setRequestBody(nvps);

      client.executeMethod(post);
      result = post.getResponseBodyAsString();
    } catch (Exception e) {
      throw new NetException("HttpClient catch!", e);
    } finally {
      if (post != null) post.releaseConnection();
    }
    return result;
  }
コード例 #7
0
ファイル: RequestAnswer.java プロジェクト: antmarpen/TFG
  private static void setValues(
      URL url,
      HttpResponse<InputStream> jsonResponse,
      AbstractMap<String, String> localHeaders,
      Long responseTime) {
    generalInfo = new HashMap<>();

    int responseCode = jsonResponse.getStatus();

    generalInfo.put("Protocol", url.getProtocol());
    generalInfo.put("Authority", url.getAuthority());
    generalInfo.put("Host", url.getHost());
    generalInfo.put("Default Port", Integer.toString(url.getDefaultPort()));
    generalInfo.put("Port ", Integer.toString(url.getPort()));
    generalInfo.put("Path", url.getPath());
    generalInfo.put("Query", url.getQuery());
    generalInfo.put("Filename", url.getFile());
    generalInfo.put("Ref", url.getRef());

    responseValues.resetProperties();
    responseValues.setRequestHeaders(localHeaders);
    responseValues.setResponseHeaders(jsonResponse.getHeaders());
    responseValues.setGeneralInfo(generalInfo);
    responseValues.setResponseTime(responseTime);
    responseValues.setResponseCode(
        Integer.toString(responseCode) + " " + jsonResponse.getStatusText());
  }
コード例 #8
0
ファイル: ServerMgr.java プロジェクト: karianna/jdk8_tl
  /*
   * Creates a HttpContext at the given address. If there is already a server
   * it uses that server to create a context. Otherwise, it creates a new
   * HTTP server. This sever is added to servers Map.
   */
  /*package*/ HttpContext createContext(String address) {
    try {
      HttpServer server;
      ServerState state;
      URL url = new URL(address);
      int port = url.getPort();
      if (port == -1) {
        port = url.getDefaultPort();
      }
      InetSocketAddress inetAddress = new InetSocketAddress(url.getHost(), port);
      synchronized (servers) {
        state = servers.get(inetAddress);
        if (state == null) {
          logger.fine("Creating new HTTP Server at " + inetAddress);
          // Creates server with default socket backlog
          server = HttpServer.create(inetAddress, 0);
          server.setExecutor(Executors.newCachedThreadPool());
          String path = url.toURI().getPath();
          logger.fine("Creating HTTP Context at = " + path);
          HttpContext context = server.createContext(path);
          server.start();

          // we have to get actual inetAddress from server, which can differ from the original in
          // some cases.
          // e.g. A port number of zero will let the system pick up an ephemeral port in a bind
          // operation,
          // or IP: 0.0.0.0 - which is used to monitor network traffic from any valid IP address
          inetAddress = server.getAddress();

          logger.fine("HTTP server started = " + inetAddress);
          state = new ServerState(server, path);
          servers.put(inetAddress, state);
          return context;
        }
      }
      server = state.getServer();

      if (state.getPaths().contains(url.getPath())) {
        String err =
            "Context with URL path "
                + url.getPath()
                + " already exists on the server "
                + server.getAddress();
        logger.fine(err);
        throw new IllegalArgumentException(err);
      }

      logger.fine("Creating HTTP Context at = " + url.getPath());
      HttpContext context = server.createContext(url.getPath());
      state.oneMoreContext(url.getPath());
      return context;
    } catch (Exception e) {
      throw new ServerRtException("server.rt.err", e);
    }
  }
コード例 #9
0
 public static String getURLOriginString(URL paramURL) {
   if (paramURL == null) return null;
   StringBuffer localStringBuffer = new StringBuffer(ORIGIN_PREFIX);
   localStringBuffer.append(paramURL.getProtocol());
   localStringBuffer.append("://");
   localStringBuffer.append(paramURL.getHost());
   localStringBuffer.append(":");
   int i = paramURL.getPort();
   if (i == -1) i = paramURL.getDefaultPort();
   localStringBuffer.append(i);
   return localStringBuffer.toString();
 }
コード例 #10
0
ファイル: URLDemo.java プロジェクト: yangjun2/java
 /**
  * @param args
  * @throws MalformedURLException
  */
 public static void main(String[] args) throws MalformedURLException {
   // TODO Auto-generated method stub
   URL url = new URL("http://www.baidu.com");
   URL tuto = new URL(url, "tutorial.intro.html#DOWNLOADING");
   System.out.println("protocal=" + tuto.getProtocol());
   System.out.println("host=" + tuto.getHost());
   System.out.println("port=" + tuto.getPort());
   System.out.println("Authority=" + tuto.getAuthority());
   System.out.println("Path=" + tuto.getPath());
   System.out.println("UserInfo=" + tuto.getUserInfo());
   System.out.println("DefaultPort=" + tuto.getDefaultPort());
 }
コード例 #11
0
ファイル: URL1.java プロジェクト: xhbang/java-class
 public static void main(String[] args) throws IOException {
   URL url = new URL("http://www.javajeff.com/articles/articles/html");
   System.out.println("Authority = " + url.getAuthority());
   System.out.println("Default port = " + url.getDefaultPort());
   System.out.println("File = " + url.getFile());
   System.out.println("Host = " + url.getHost());
   System.out.println("Path = " + url.getPath());
   System.out.println("Port = " + url.getPort());
   System.out.println("Protocol = " + url.getProtocol());
   System.out.println("Query = " + url.getQuery());
   System.out.println("Ref = " + url.getRef());
   System.out.println("User Info = " + url.getUserInfo());
 }
コード例 #12
0
 public THttpClient(String url, HttpClient client) throws TTransportException {
   try {
     url_ = new URL(url);
     this.client = client;
     this.host =
         new HttpHost(
             url_.getHost(),
             -1 == url_.getPort() ? url_.getDefaultPort() : url_.getPort(),
             url_.getProtocol());
   } catch (IOException iox) {
     throw new TTransportException(iox);
   }
 }
コード例 #13
0
 private void _log(@Nonnull final URL aURL) throws URISyntaxException {
   s_aLogger.info("Next URL");
   s_aLogger.info("  protocol = " + aURL.getProtocol());
   s_aLogger.info("  authority = " + aURL.getAuthority());
   s_aLogger.info("  host = " + aURL.getHost());
   s_aLogger.info("  port = " + aURL.getPort());
   s_aLogger.info("  defaultPort = " + aURL.getDefaultPort());
   s_aLogger.info("  path = " + aURL.getPath());
   s_aLogger.info("  query = " + aURL.getQuery());
   s_aLogger.info("  file = " + aURL.getFile());
   s_aLogger.info("  ref = " + aURL.getRef());
   s_aLogger.info("  externalForm = " + aURL.toExternalForm());
   s_aLogger.info("  URI          = " + aURL.toURI().toString());
 }
コード例 #14
0
ファイル: Browser.java プロジェクト: Lujango/frostwire
 /**
  * TRies to get a fuill url out of string
  *
  * @throws BrowserException
  */
 public String getURL(String string) throws BrowserException {
   if (string == null) {
     string = this.getRedirectLocation();
   }
   if (string == null) {
     throw new BrowserException("Null URL");
   }
   try {
     new URL(string);
   } catch (final Exception e) {
     if (this.request == null || this.request.getHttpConnection() == null) {
       return string;
     }
     final String base = this.getBase(string);
     if (string.startsWith("/") || string.startsWith("\\")) {
       try {
         final URL bUrl = new URL(base);
         String proto = "http://";
         if (base.startsWith("https")) {
           proto = "https://";
         }
         String portUse = "";
         if (bUrl.getDefaultPort() > 0
             && bUrl.getPort() > 0
             && bUrl.getDefaultPort() != bUrl.getPort()) {
           portUse = ":" + bUrl.getPort();
         }
         string = proto + new URL(base).getHost() + portUse + string;
       } catch (final MalformedURLException e1) {
         e1.printStackTrace();
       }
     } else {
       string = base + string;
     }
   }
   return Browser.correctURL(Encoding.urlEncode_light(string));
 }
コード例 #15
0
  public static URL concat(URL baseUrl, String url) throws MalformedURLException {
    if (absoluteUrlPattern.matcher(url).matches()) {
      return new URL(url);
    }

    int lastSlash = baseUrl.toExternalForm().lastIndexOf("/");
    if (lastSlash == -1) {
      return new URL(url);
    }

    int firstSlash = url.indexOf("/");
    if (firstSlash == 0) {
      boolean portSet = (baseUrl.getDefaultPort() == baseUrl.getPort() || baseUrl.getPort() == -1);
      String port = portSet ? "" : ":" + baseUrl.getPort();
      return new URL(baseUrl.getProtocol() + "://" + baseUrl.getHost() + port + url);
    } else {
      return new URL(baseUrl.toExternalForm().substring(0, lastSlash + 1) + url);
    }
  }
コード例 #16
0
    /** @return */
    public String build() {
      String path = prefixPath(baseUrl.getPath(), this.path);
      int port = baseUrl.getPort();
      if (baseUrl.getPort() == baseUrl.getDefaultPort()) port = -1;

      String host = baseUrl.getHost();
      if (StringUtils.isNotBlank(host) && host.startsWith(".")) host = host.substring(1);

      final StringBuilder builder;
      try {
        builder = new StringBuilder(new URL(baseUrl.getProtocol(), host, port, path).toString());
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }

      StringBuilder query = new StringBuilder();
      for (Entry<String, Object> entry : urlParams.entrySet()) {
        final String key = entry.getKey();
        Object value = entry.getValue();
        if (value == null) continue;

        if (value instanceof Object[]) {
          for (final Object v : (Object[]) value) {
            appendQueryString(key, v, query);
          }
        } else if (value instanceof Collection) {
          for (final Object v : (Collection<?>) value) {
            appendQueryString(key, v, query);
          }
        } else appendQueryString(key, value, query);
      }
      if (query.length() > 0) query.replace(0, 1, "?");
      builder.append(query);

      if (values.get() != null && values.get().size() > 0)
        return MessageFormat.format(builder.toString(), values.get().toArray());
      return builder.toString();
    }
コード例 #17
0
  /* (non-Javadoc)
   * @see java.net.URLConnection#getInputStream()
   */
  @Override
  public InputStream getInputStream() throws IOException {
    try {
      if (m_client == null) {
        connect();
      }
      int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
      String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":");

      HttpGet request =
          new HttpGet(
              URIUtils.createURI(
                  m_url.getProtocol(),
                  m_url.getHost(),
                  port,
                  m_url.getPath(),
                  m_url.getQuery(),
                  null));
      if (userInfo != null) {
        UsernamePasswordCredentials credentials =
            new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
        request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false));
      }
      HttpResponse response = m_client.execute(request);
      return response.getEntity().getContent();
    } catch (Exception e) {
      throw new IOException(
          "Can't retrieve "
              + m_url.getPath()
              + " from "
              + m_url.getHost()
              + " because "
              + e.getMessage(),
          e);
    }
  }
コード例 #18
0
ファイル: BasicURLNormalizer.java プロジェクト: Ccccz/nutch
  public String normalize(String urlString, String scope) throws MalformedURLException {

    if ("".equals(urlString)) // permit empty
    return urlString;

    urlString = urlString.trim(); // remove extra spaces

    URL url = new URL(urlString);

    String protocol = url.getProtocol();
    String host = url.getHost();
    int port = url.getPort();
    String file = url.getFile();

    boolean changed = false;

    if (!urlString.startsWith(protocol)) // protocol was lowercased
    changed = true;

    if ("http".equals(protocol) || "https".equals(protocol) || "ftp".equals(protocol)) {

      if (host != null) {
        String newHost = host.toLowerCase(); // lowercase host
        if (!host.equals(newHost)) {
          host = newHost;
          changed = true;
        }
      }

      if (port == url.getDefaultPort()) { // uses default port
        port = -1; // so don't specify it
        changed = true;
      }

      if (file == null || "".equals(file)) { // add a slash
        file = "/";
        changed = true;
      }

      if (url.getRef() != null) { // remove the ref
        changed = true;
      }

      // check for unnecessary use of "/../", "/./", and "//"
      String file2 = getFileWithNormalizedPath(url);
      if (!file.equals(file2)) {
        changed = true;
        file = file2;
      }
    }

    // properly encode characters in path/file using percent-encoding
    String file2 = unescapePath(file);
    file2 = escapePath(file2);
    if (!file.equals(file2)) {
      changed = true;
      file = file2;
    }

    if (changed) urlString = new URL(protocol, host, port, file).toString();

    return urlString;
  }
コード例 #19
0
  public String normalize(String url) {
    String result = url.trim();

    // First see if there is any protocol - if not, append http:// by default.
    if (result.indexOf("://") == -1) {
      // FUTURE - could put some limit on max length of protocol string.
      result = "http://" + result;
    }

    // Danger, hack! Some sites have session ids that look like
    // http://domain.com/page.html;jsessionid=xxx,
    // or even http://domain.com/page.html;jsessionid=xxx&q=z. So we always want to try to get rid
    // of
    // session ids first, before doing any other processing.
    Matcher matcher = SESSION_ID_PATTERN.matcher(result);
    if (matcher.find()) {
      result =
          result.substring(0, matcher.start()) + matcher.group(4) + result.substring(matcher.end());
    }

    URL testUrl;

    try {
      String decodedUrl = result.replace("+", "%20");
      testUrl = new URL(decodedUrl);
      url = testUrl.toExternalForm();
    } catch (MalformedURLException e) {
      // Not a valid URL we know about, so in this case we're just going to
      // return it as-is, other than the stripping we did.
      LOGGER.trace("Malformed URL being returned without further processing: " + result);
      return result;
    }

    // Don't do additional special processing for anything other than http/https protocols.
    String protocol = testUrl.getProtocol().toLowerCase();
    if (!protocol.equals("http") && !protocol.equals("https")) {
      return result;
    }

    String hostname = normalizeHostname(testUrl.getHost());

    int port = testUrl.getPort();
    if (port == testUrl.getDefaultPort()) {
      port = -1;
    }

    String path = normalizePath(testUrl.getPath());

    // Danger, hack! Some sites (like StumbleUpon) use anchor text as query text, so they
    // have a URL that looks like http://www.stumbleupon.com/toolbar/#url=...
    // Assume that if the first '#' is preceded by a '/', and that '#' is our anchor text,
    // then we want to include it versus stripping it out. But only do this if the caller
    // explicitly wants that behavior, as most sites use .../#<whatever> for dynamic navigation.

    // FUTURE KKr - better would be to not require special param, and instead always see if the
    // ref looks like a query, in that there's one or more <key>=<value> pairs separated by '&'.
    String query = testUrl.getQuery();
    String anchor = testUrl.getRef();

    int pos = url.indexOf("#" + anchor);
    if (_treatRefAsQuery
        && (anchor != null)
        && (query == null)
        && (pos != -1)
        && (url.charAt(pos - 1) == '/')) {
      anchor = "#" + normalizeQuery(anchor);
      query = "";
    } else {
      anchor = "";
      query = normalizeQuery(query);

      if (query.length() > 0) {
        query = "?" + query;
      }
    }

    try {
      testUrl = new URL(protocol, hostname, port, path + query + anchor);
    } catch (MalformedURLException e) {
      // Should never happen, so just log it and return current result
      LOGGER.error("Unexpected exception during normalization", e);
      return result;
    }

    return testUrl.toExternalForm();
  }
コード例 #20
0
 /* (non-Javadoc)
  * @see java.net.URLConnection#getInputStream()
  */
 @Override
 public InputStream getInputStream() throws IOException {
   try {
     if (m_client == null) {
       connect();
     }
     // Build URL
     int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
     URIBuilder ub = new URIBuilder();
     ub.setPort(port);
     ub.setScheme(m_url.getProtocol());
     ub.setHost(m_url.getHost());
     ub.setPath(m_url.getPath());
     ub.setQuery(m_url.getQuery());
     // Build Request
     HttpRequestBase request = null;
     if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) {
       final Content cnt = m_request.getContent();
       HttpPost post = new HttpPost(ub.build());
       ContentType contentType = ContentType.create(cnt.getType());
       LOG.info("Processing POST request for %s", contentType);
       if (contentType
           .getMimeType()
           .equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
         FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData());
         post.setEntity(fields.getEntity());
       } else {
         StringEntity entity = new StringEntity(cnt.getData(), contentType);
         post.setEntity(entity);
       }
       request = post;
     } else {
       request = new HttpGet(ub.build());
     }
     if (m_request != null) {
       // Add Custom Headers
       for (Header header : m_request.getHeaders()) {
         request.addHeader(header.getName(), header.getValue());
       }
     }
     // Add User Authentication
     String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":");
     if (userInfo != null) {
       UsernamePasswordCredentials credentials =
           new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
       request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false));
     }
     // Get Response
     HttpResponse response = m_client.execute(request);
     return response.getEntity().getContent();
   } catch (Exception e) {
     throw new IOException(
         "Can't retrieve "
             + m_url.getPath()
             + " from "
             + m_url.getHost()
             + " because "
             + e.getMessage(),
         e);
   }
 }
コード例 #21
0
  public static MyMessage Post(String url, String data) {
    CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "URL = " + url);
    CobubLog.d(
        UmsConstants.LOG_TAG,
        NetworkUtil.class,
        "LENGTH:" + data.length() + " *Data = " + data + "*");

    if (!hasInitSSL && UmsConstants.SDK_SECURITY_LEVEL.equals("2")) {
      initSSL();
      hasInitSSL = true;
    }

    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
    DefaultHttpClient httpclient = null;

    /*SDK会运行在如下两种环境中:
     * 1,CPOS:需要进行双向ssl校验;SDK_SSL=true;此时还要验证dn(如果不想验dn,设置SDK_HTTPS_DN为none)
     * 2,一般移动设备,是用HTTPS正常发送即可,也能接受非标准证书的https服务端
     * 3,测试:使用http
     */
    if (UmsConstants.SDK_SECURITY_LEVEL.equals("2")) {
      httpclient = new DefaultHttpClient(httpParams);
      // cpos with dn check
      if (!UmsConstants.SDK_HTTPS_DN.equals("none")) {
        SSLSocketFactory mysf = null;
        try {
          mysf = new CposSSLSocketFactory();
          if (serverUrl == null) {
            serverUrl = new URL(url);
            serverPort =
                ((serverUrl.getPort() == -1) ? serverUrl.getDefaultPort() : serverUrl.getPort());
          }

          httpclient
              .getConnectionManager()
              .getSchemeRegistry()
              .register(new Scheme(serverUrl.getProtocol(), mysf, serverPort));

        } catch (Exception e) {
          CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, e.toString());
        }
      }
    } else if (UmsConstants.SDK_SECURITY_LEVEL.equals("1")
        && url.toLowerCase().startsWith("https")) {
      // for https with company cert
      if (serverPort < 0) {
        serverPort = getPort();
      }
      CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "InitSSL port is:" + serverPort);
      SchemeRegistry schReg = new SchemeRegistry();
      schReg.register(new Scheme("https", SSLCustomSocketFactory.getSocketFactory(), serverPort));

      ClientConnectionManager connMgr = new ThreadSafeClientConnManager(httpParams, schReg);
      httpclient = new DefaultHttpClient(connMgr, httpParams);
    } else {
      httpclient = new DefaultHttpClient(httpParams);
    }
    processCookieRejected(httpclient);

    MyMessage message = new MyMessage();
    try {
      HttpPost httppost = new HttpPost(url);

      StringEntity se = new StringEntity("content=" + URLEncoder.encode(data), HTTP.UTF_8);
      se.setContentType("application/x-www-form-urlencoded");
      httppost.setEntity(se);

      HttpResponse response = httpclient.execute(httppost);
      CobubLog.d(
          UmsConstants.LOG_TAG,
          NetworkUtil.class,
          "Status code=" + response.getStatusLine().getStatusCode());

      String returnXML = EntityUtils.toString(response.getEntity());
      int status = response.getStatusLine().getStatusCode();
      String returnContent = URLDecoder.decode(returnXML, "UTF-8");
      CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "returnString = " + returnContent);
      // TODO:只要服務端有返回200ok并且返回的是json字符串即可认为发送成功;因为如果发送的数据不完整服务端会返回flag<0;
      // 这部分数据按照flag来判断会导致错误数据始终保存在本地
      switch (status) {
        case 200:
          message.setSuccess(isJson(returnContent));
          message.setMsg(returnContent);
          break;
        default:
          Log.e("error", status + returnContent);
          message.setSuccess(false);
          message.setMsg(returnContent);
          break;
      }
    } catch (Exception e) {
      message.setSuccess(false);
      message.setMsg(e.toString());
    }
    return message;
  }
コード例 #22
0
 public HttpClientContext decoratePrototypeContext(final URL url, final HttpClientContext ctx) {
   final AuthScope as =
       new AuthScope(url.getHost(), url.getPort() < 0 ? url.getDefaultPort() : url.getPort());
   return decoratePrototypeContext(as, null, null, ctx);
 }