read url content WHILE it is continuously updating - java

I have a php script that makes a few api calls in a row and once an api call has
returned data the script outputs the content. Meaning if you call the script on
the browser you will wait 1 second then see some content appear, than after 2 seconds
more content will be appended to the page and so on.
The thing is I am accessing this content from java/android in one of my apps.
Is there a way I can read this content from java WHILE it is updating? This
way I will populate the application content as new data is being fetched from
the script.
I have tried something like this when I have accessed xml files but they
were not continuously updating.
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
}

Yes, use URLConnection instead of HttpClient. Here's a tutorial.
Like in the tutorial, connection.getInputStream() will return a stream you can read and process, for example line-wise.

Related

Selenium Script To Wrap The calls Java

i am learning about wrappers and wanted to know how to achieve this task .
Problem : We need selenium script to wrap the calls we have in the attached file.
each call (e.g. fetch) should be a line in the selenium script. quite straight forward.
Now Lines look like this :
fetch("http://5.9.140.34:8081/secure/ConfigureTeamsAction.jspa?rapidView=15", {"credentials":"include","headers":{"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3","accept-language":"en-US,en;q=0.9,he;q=0.8","cache-control":"no-cache","pragma":"no-cache","upgrade-insecure-requests":"1"},"referrer":"http://5.9.140.34:8081/secure/RapidBoard.jspa?rapidView=15&view=planning.nodetail&issueLimit=100","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"}); ;
fetch("http://5.9.140.34:8081/s/51a7afa49655351e0828b5c02eddafcd-CDN/9jmibt/802003/7d9ab5157a35650ecc126b476125fb4a/2215ce00ef54a85f6638f0246e6582bf/_/download/contextbatch/css/_super/batch.css", {"credentials":"include","headers":{"accept":"text/css,*/*;q=0.1","accept-language":"en-US,en;q=0.9,he;q=0.8","cache-control":"no-cache","pragma":"no-cache"},"referrer":"http://5.9.140.34:8081/secure/ConfigureTeamsAction.jspa?rapidView=15","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"}); ;
I am having problems what should Selenium wrapper do after reading these lines one by one .. according to above problem statement ?
I have done something in java :
DefaultHttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost("http://some.url");
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("paramName", "paramValue"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
But i am not sure how to send requests using Selenium

HttpClient redirecting

I am trying to write a video downloader from kissanime.to . I am using HttpClient library. This site is using cloudflare. It redirects after 5 secs. How can I set so my application will go to the redirected link? My code below isnt working. Where am I going wrong and how can I fix it.
HttpGet request = new HttpGet(url);
HttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String entityContents = EntityUtils.toString(entity);`
It means that given site is under DDOS protection mode (maybe you try to open it too often?) to workaround you would need to stop hitting it that much (e.g. wait some time between tries). Or if you insist use some javascript executing library (Rhino?) that would execute javascript that cloudflare is using.

Apache HTTPClient HttpGet returning nothing

I'm doing a GET request with version 4.3.3 of Apache HttpClient, like this:
HttpGet httpGet = new HttpGet("http://www.revenue.ie/en/tax/it/forms/med1.pdf");
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpGet);
client.close();
The response status code tells me 200, and the content length as returned by response.getEntity().getContentLength() is 1213954, but the InputStream as returned from a call to:
response.getEntity().getContent()
...is reporting 0 bytes available.
I have been successfully making GET calls like this to retrieve and parse the HTML of other URLs, but is there something different I need to do here since it's file contents that I'm interested in?
The problem was that I was closing the http client too early i.e. client.close() before I tried to retrieve the response InputStream with a call to response.getEntity().getContent().

Apache Httppost retrieve image from server Java

Right now I am using Httppost to Post some parameters in the form of xml to a server. When the post occurs, a geotiff or .tif file is downloaded. I have successfully posted the document to the server and successfully downloaded the file simply by attaching the parameters to the url but I can't seem to combine the two. I have to use post because just using the URL leaves out elevation data in the geotiff.
In short, I am not sure how to simultaneously post and retrieve the image of the post. This is what I have thus far...
// Get target URL
String strURL = POST;
// Get file to be posted
String strXMLFilename = XML_PATH;
File input = new File(strXMLFilename);
// Prepare HTTP post
HttpPost post = new HttpPost(strURL);
post.setEntity(new InputStreamEntity(
new FileInputStream(input), input.length()));
// Specify content type and encoding
post.setHeader(
"Content-type", "text/xml");
// Get HTTP client
HttpClient httpclient = new DefaultHttpClient();
//Locate file to store data in
FileEntity entity = new FileEntity(newTiffFile, ContentType.create("image/geotiff"));
post.setEntity(entity);
// Execute request
try {
System.out.println("Connecting to Metoc site...\n");
HttpResponse result = httpclient.execute(post);
I was under the impression that the entity would contain the resulting image. Any help is much appreciated!
Thanks for the help guys. The entity was what was being sent to the server. I had code that was trying to read it from the response as well but it wasn't working because setting the entity to a file entity messed up the post request. By removing that part, it works great!

How to send a string to a php file from Android?

I have a php script on a local server that creates and manages an SQL table. I am able to create the table and database through my android app but I am having trouble figuring out how to send data to the php file. I want to send a string so that I can sort and pick the values to return to my app.
How do I change my php and android code so that I can get entries in the table between 2 dates?
Here is some of my Android code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.3/xampp/information.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
The beginning of information.php script is setup like this:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data",$con);
How do I send a string from Android to the php file?
You should try using $_POST variables. So your HttpPost object would be initialized like this:
HttpPost httppost = new HttpPost("http://10.0.0.3/xampp/information.php?info="+nameValuePairs);
Then, in your PHP, just check to see if the variable info is set, and then process it if it is.
if(isset(_$POST['info']))
//process data
This would, of course, requiring some formatting of nameValuePairs so that it is a valid URL, but it forces everything into one variable which you can easily check in your PHP.

Categories