This question already has answers here:
URL encoding in Android
(7 answers)
Closed 9 years ago.
I am newbie in android, i have this url now i need to encode this url: the url that has to be encoded.
With Reference of downloading apk in Mobile using Java how can i pass this url to this Line :
updateAppInstance.execute("http://demo.ingresssolutions.com/proposalmanagement/services/user/getApkFile");
Previous post Regarding Apk Download
http://demo.ingresssolutions.com/proposalmanagement/services/user/uploadFile
Contrary to many answers you will read, java.net.URLEncoder isn't for encoding URLs. It is for encoding URL and POST arguments.
The correct way to encode a URL in Java is as given in this answer, or, if you only need to encode the path component:
String encodedPath = new URI(null, path, null).toASCIIString();
import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import java.io.UnsupportedEncodingException;
public class Encoder{
public static void main(String[] args){
URL url = null;
try{
url = new URL("http://www.stackoverflow.com");
}catch(MalformedURLException mue){
System.err.println(mue);
}
System.out.println(url + "
");
try{
String encodedurl = URLEncoder.encode(url.toString(),"UTF-8");
System.out.println(encodedurl);
}catch(UnsupportedEncodingException uee){
System.err.println(uee);
}
}
}
Hope this helps you
Related
This question already has answers here:
URL Encode and Decode Special character in Java
(4 answers)
Closed 11 months ago.
I am new to java and i have characters like this
"%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72"
When i try to convert them using this tool. i am able to convert to the values as desired.
converted results :
"maa ward lo enni sarlu house site koraku apply chesina ma'am etuvanti upayogam lekunda individual poyindi try cheyamantaraa vadileyamantaara please send me answer"
but how can i do it using programmatically can someone help.
I tried this
String raw = "%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72";
String searchSection = new String(raw.getBytes(StandardCharsets.UTF_8), "UTF-8");
System.out.println( searchSection);
if this question is duplicate of any other question please point me in the right direction thanks
Converted text should match
You can achieve it using URLDecoder.decode(yourMessage, "UTF-8") from java.net:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class Main {
public static void main(String[] args) {
String yourMessage = "%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72";
try {
System.out.println( URLDecoder.decode(yourMessage, "UTF-8"));
} catch (UnsupportedEncodingException uee) { }
}
}
Output:
maa ward lo enni sarlu house site koraku apply chesina ma'am etuvanti upayogam lekunda individual poyindi try cheyamantaraa vadileyamantaara please send me answer
This question already has answers here:
File to byte[] in Java
(27 answers)
Closed 6 years ago.
So, I have this .mp4 video file which I would like to convert into bytes and send it to my client.
At client, I'll receive all the bytes over RTP and then construct my own .mp4 file.
Please help me doing this, I'm not posting any code because, I don't know where to start and I'm all new to file handling in java
Thanks
You can use the Apache commons IOUtils.toByteArray method to create a byte array from an InputStream
Example:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
class ConvertToByteArray
{
public static void main(String args[])
{
FileInputStream is = null;
try
{
is = new FileInputStream("file.mp4");
byte [] byteArr = IOUtils.toByteArray(is);
}
catch (IOException ioe)
{}
finally
{
// close things
}
}
}
You can download the jar her at Apache Commons IO
This question already has answers here:
How can I download and save a file from the Internet using Java?
(23 answers)
Closed 7 years ago.
I am using Yahoo's URL request ability to download stock data. I have found a java application that downloads the data every 5 seconds. I cant figure out how one would go about downloading stock data to a certain folder in ones computer while using the basic code I have used in the application. There has been other posts about how to save yahoo URL request data to a certain location but it does not use the same code format that I'm using. All ideas, source code, and links explaining how I should do this would be very helpful. Thank you for your help!
My code:
import java.awt.Desktop;
import java.net.URL;
public class Downloader {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread.sleep(5000);
try {
Desktop.getDesktop()
.browse(new URL(
"http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab")
.toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
you could use Java's IO capabilities, Are you trying to write a GUI, if not any specific reason to use AWT's Desktop class?
Try this sample code:
URL yahooFinance = new URL("http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab");
ReadableByteChannel channel = Channels.newChannel(yahooFinance.openStream());
FileOutputStream fos = new FileOutputStream("quotes.csv");
fos.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
You can also use org.apache.commons.io.FileUtils
java.net.URL yahooFinance = new java.net.URL("http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab");
File f = new File("quotes.csv");
FileUtils.copyURLToFile(yahooFinance, f);
This question already has answers here:
How to code an automated bot that can browse and do operations on a webpage
(6 answers)
Closed 7 years ago.
I am new to writing code and I am trying to write code to scrape a specific website. The issue is that this website has a page to accept the conditions of use and privacy page. This can be seen by the website: http://cpdocket.cp.cuyahogacounty.us/
I need to bypass this page somehow and I have no idea how. I am writing my code in Java, and so far have working code that scrapes the source for any website. This code is:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.io.IOException;
// Scraper class takes an input of a string, and returns the source code of the of the website
public class Scraper {
private static String url; // the input website to be scraped
//constructor
public Scraper(String url) {
this.url = url;
}
//scrapeWebsite runs the method to scrape the input variable. As of now it retuns a string. This string idealy should be saved
//so it is able to be parsed by another method
public static String scrapeWebsite() throws IOException {
URL urlconnect = new URL(url); //creates the url from the variable
URLConnection connection = urlconnect.openConnection(); // connects to the created url
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8")); // annonymous class to stream the website
String inputLine; //creates a new variable of string
StringBuilder a = new StringBuilder(); // creates stringbuilder
//loop appends to the string builder as long as there is information
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
}
Any suggestions on how to go about doing this would be greatly appreciated.
I am rewriting the code based off a ruby code. The code is:
def initializeSession()
## SETUP # POST headers
post_header = Hash.new()
post_header['Host'] = 'cpdocket.cp.cuyahogacounty.us'
post_header['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0'
post_header['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
post_header['Accept-Language'] = 'en-US,en;q=0.5'
post_header['Accept-Encoding'] = 'gzip, deflate'
post_header['X-Requested-With'] = 'XMLHttpRequest'
post_header['X-MicrosoftAjax'] = 'Delta=true'
post_header['Cache-Control'] = 'no-cache'
post_header['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
post_header['Referer'] = 'http://cpdocket.cp.cuyahogacounty.us/Search.aspx' # may have to alter this per request
# post_header['Content-Length'] = '12197'
post_header['Connection'] = 'keep-alive'
post_header['Pragma'] = 'no-cache'
# STEP # set up simulated browser and make first request
#browser = SimBrowser.new()
#logname = 'log.txt'
#s = Scribe.new(logname)
session_cookie = 'ASP.NET_SessionId'
url = 'http://cpdocket.cp.cuyahogacounty.us/'
#browser.http_get(url)
#puts browser.get_body() # debug
puts 'DEBUG: session cookie: ' + #browser.get_cookie_var(session_cookie)
#log.slog('DEBUG: home page response code: expected 200, actual ' + #browser.get_response().code)
# s.flog('### HOME PAGE RESPONSE')
# s.flog(browser.get_body()) # debug
# STEP # send our acceptance of the terms of service
data = {
'ctl00$SheetContentPlaceHolder$btnYes' => 'Yes',
'__EVENTARGUMENT'=>'',
'__EVENTTARGET'=>'',
'__EVENTVALIDATION'=>'/wEWBwKc78CQCQLn3/HqCQLZw/fZCgLipuudAQK42duKDQL33NjnAwKn6+K4CIM3TSmrbrsn2xBRJf2DRwg01Vsbdk+oJV9lhG/in+xD',
'__VIEWSTATE'=>'/wEPDwUKLTI4MzA1ODM0OA9kFgJmD2QWAgIDD2QWDgIDD2QWAgIBD2QWCAIBDxYCHgRUZXh0BQ9BbmRyZWEgRi4gUm9jY29kAgMPFgIfAAUfQ3V5YWhvZ2EgQ291bnR5IENsZXJrIG9mIENvdXJ0c2QCBQ8PFgIeB1Zpc2libGVoZGQCBw8PFgIfAWhkZAIHDw9kFgIeB29uY2xpY2sFGmphdmFzY3JpcHQ6d2luZG93LnByaW50KCk7ZAILDw9kFgIfAgUiamF2YXNjcmlwdDpvbkNsaWNrPXdpbmRvdy5jbG9zZSgpO2QCDw8PZBYCHwIFRmRpc3BsYXlQb3B1cCgnaF9EaXNjbGFpbWVyLmFzcHgnLCdteVdpbmRvdycsMzcwLDIyMCwnbm8nKTtyZXR1cm4gZmFsc2VkAhMPZBYCZg8PFgIeC05hdmlnYXRlVXJsBRMvVE9TLmFzcHg/aXNwcmludD1ZZGQCFQ8PZBYCHwIFRWRpc3BsYXlQb3B1cCgnaF9RdWVzdGlvbnMuYXNweCcsJ215V2luZG93JywzNzAsMzcwLCdubycpO3JldHVybiBmYWxzZWQCFw8WAh8ABQYxLjAuNTRkZEnXSWiVLEPsDmlc7dX4lH/53vU1P1SLMCBNASGt4T3B'
}
#post_header['Referer'] = url
#browser.http_post(url, data, post_header)
#log.slog('DEBUG: accept terms response code: expected 200, actual ' + #browser.get_response().code)
#log.flog('### TOS ACCPTANCE RESPONSE')
# #log.flog(#browser.get_body()) # debug
end
can this be done in Java as well?
If you don't understand how to do this, the best way to learn is to do this manually while watching what happens with FireBug (on Firefox) or the equivalent tools for IE, Chrome or Safari.
You must duplicate in your code whatever happens in the protocol when the user accepts the terms & conditions manually.
You must also be aware that the UI presented to the user may not be sent directly as HTML, it may be constructed dynamically by Javascript that would normally run on the browser. If you are not prepared to fully emulate a browser to the point of maintaining a DOM and executing Javascript, then this may not be possible.
How can I programmatically through java convert an image to "some string" to pass it as a parameter for searching in google image search. Actually I have made some base64 convertion of image but it differs from that that google does in its image search engine. I've made such a convertion(java 7):
import javax.xml.bind.DatatypeConverter;
...
Path p = Paths.get("my_photo.JPG");
try(InputStream in = Files.newInputStream(p);
PrintWriter write = new PrintWriter("base64.txt");
) {
byte [] bytes = new byte[in.available()];
in.read(bytes);
String base64 = DatatypeConverter.printBase64Binary(bytes);
write.println(base64);
} catch(IOException ex) {
ex.printStackTrace();
}
the output of this simple program differs from the google's string in url. I talk about that string that goes after tbs=sbi:AMhZZ...
This is my best guess for how the image search works:
The data in the URL is not an encoded form of the image. The data is an image fingerprint used for fuzzy matching.
You should notice that when you upload an image for searching, it is a 2 step process. The first step uploads the image via the url http://images.google.com/searchbyimage/upload. The Google server returns the fingerprint. The browser is then redirected to a search page with a query string based on the fingerprint.
Unless Google publishes the algorithm for generating the fingerprint, you will be unable to generate the search query string from within your application. Until then, you can have your application post the image to the upload URI. You should be able to parse the response and construct the query string.
EDIT
These are the keys and values sent to the server when I uploaded a file.
image_url =
btnG = Search
encoded_image = // the binary image content goes here
image_content =
filename =
hl = en
bih = 507
biw = 1920
"bih" and "biw" look like dimensions, but do not corrispond to the uploaded file.
Use this information at your own risk. It is an undocumented api that could change and break your application.
Using google's image search.
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpFileUpload {
public static void main(String args[]){
try {
HttpClient client = new DefaultHttpClient();
String url="https://www.google.co.in/searchbyimage/upload";
String imageFile="c:\\temp\\shirt.jpg";
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
entity.addPart("encoded_image", new FileBody(new File(imageFile)));
entity.addPart("image_url",new StringBody(""));
entity.addPart("image_content",new StringBody(""));
entity.addPart("filename",new StringBody(""));
entity.addPart("h1",new StringBody("en"));
entity.addPart("bih",new StringBody("179"));
entity.addPart("biw",new StringBody("1600"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.indexOf("HREF")>0)
System.out.println(line.substring(8));
}
}catch (ClientProtocolException cpx){
cpx.printStackTrace();
}catch (IOException ioex){
ioex.printStackTrace();
}
}
}
Based on #Ajit's answer, this does the same but using the curl command (Linux / Cygwin / etc)
curl -s -F "image_url=" -F "image_content=" -F "filename=" -F "h1=en" -F "bih=179" -F "biw=1600" -F "encoded_image=#my_image_file.jpg" https://www.google.co.in/searchbyimage/upload
This will print a URL on standard output. You can download that URL with curl or wget but you may have to change the User Agent to that of a graphical web browser like Chrome.
This is what work for me. No need any encoding actually.
https://www.google.com/searchbyimage?image_url=YOUR_IMAGE_URL
Use Google Vision API for that. There are also lot of examples available from Google