Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query URL Question

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hi Everyone,

I know how to get the content for basic HTML pages using Java. However, when I try to get the content for something like " Java responds that I am including an illegal character (ie '?') in the URL. I know that ColdFusion can handle this sort of thing. Can Java?

If Java can handle this sort of URL, could somebody tell me how to get around the problem that I am having.

thanks
 
you need
Code:
suggestion?term=telephone.com

or

Code:
suggestion/mypage.jsp?term=telephone.com

or ... etc.

-pete

 
No, that doesn't seem to be working. If I put the '?' anywhere in the URL, I get a Java error.

The URL must be the same, at least the host must see it as the same to get the page required. When I try placing the '?' somewhere else using a browser, I get a different page.
 
Here is something I found to post data:
public void send(String data) {
try {
System.out.println("Data: " + data);

//URL encode the data to the x- MIME format
String urlEncodedData = URLEncoder.encode(data);
System.out.println("URL Encoded Data: " + urlEncodedData);

//Instance the URL (protocol, host, port)
String protocol = getCodeBase().getProtocol();
int port = getCodeBase().getPort();
String host = getCodeBase().getHost();
URL url = new URL(protocol, host, port, "/servlet/ProxyServlet");
System.out.println("Destination URL: " + url.toString());

//Open the connection
urlConnection = url.openConnection();

//Turn off the caching feature
urlConnection.setUseCaches(false);

//Set the URL to POST instead of GET
urlConnection.setDoOutput(true);

//Set necessary properties
urlConnection.setRequestProperty("content-type",
"application/x- urlConnection.setRequestProperty("content-length",
String.valueOf(urlEncodedData.length()));

//Get the output stream and send the data
PrintStream outStream =
new PrintStream(urlConnection.getOutputStream());
outStream.println(urlEncodedData);
outStream.close();
System.out.println("Data sent successfully");
}
catch(Exception e) {
System.out.println("URL Connection error: " + e.toString());
}
}


Complete code is here:


When you want to use get to send data it is a lot simpler I think you pass the querystrng in the URL.
Take a look at the set method of the URL (since 1.3)
 
I think that I've figured it out this morning. I had to make a string first, then put it into the URL. This works for queries.
 
>> I had to make a string first, then put it into the URL

could you explain or show that? How else could you put it in the URL if it's not a string?

-pete

 
I just executed the following line of code without error:

Code:
java.net.URL url = 
	new java.net.URL("[URL unfurl="true"]http://inventory.overture.com/d/searchinventory/suggestion/?term=hello");[/URL]

I believe your problem was somewhere else.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top