Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:42:49+00:00 2026-05-18T08:42:49+00:00

I feel like I must have missed the right thread, because this seems like

  • 0

I feel like I must have missed the right thread, because this seems like it would have been clearly answered before, so sorry if it’s a repeat. I read a lot of the threads here and elsewhere and I didn’t see a clear “This is how you do it” kind of answer to this question…

The program will be receiving full urls as strings. For example:
“http://www.stackoverflow.com/questions/somepage?somequery#somefragment”

I want to take this string and ensure it is properly encoded for use in a connection like with HttpClient.

From what I read, it seems like the way to do this is to use a URI() constructor with the various parts as parameters. So that then begs the question, what is the most reliable way to take a string url, that we know nothing about, other than it is a url, and break it up into the various pieces needed for the URI() constructor.

Here is what I pieced together. Are there any flaws in this method?

URL url = new URL(urlString);
URI uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), null);

Then, the uri variable contains a encoded ready to use URI. This code works so far, but I want to make sure there aren’t any pitfalls that might occur.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:42:49+00:00Added an answer on May 18, 2026 at 8:42 am

    The URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL().
    Ref: http://download.oracle.com/javase/1.5.0/docs/api/java/net/URL.html

    Note, any URL instance that complies with RFC 2396 can be converted to a URI. However, some URLs that are not strictly in compliance can not be converted to a URI.

    URI.toURL is a convenience method works as if invoking it were equivalent to evaluating the expression new URL(this.toString()) *after first checking that this URI is absolut*e.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this must have been asked before, but I've looked at Knockout
I feel like this must have been asked before, but I can't find it.
I feel like this question must have been asked before but I must not
I feel like this must have been asked, but I'm unable to find it
I feel like this must be very simple, but for some reason I am
This is such a dumb question that I feel like I must be missing
I am just starting out with Lucene, and I feel like I must have
I feel like this must be simple to do but can't get it done
I feel like this must be a common issue that I'm just struggling to
I feel like I must be missing something completely obvious, but I don't see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.