I have an online database and a simple php script with some insert statements. I want to pass variables through the URL to update the database using the php script I have created. When I run the URL on my browser it works fine and adds the values into the database, but now I want to do this with android. So far this is pretty much all I could find to help me with this:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
HttpResponse response = httpClient.execute(httpGet, localContext);
And I have no idea what to do afterwards. I've been searching for a solution but nothing I find works for me. All I need to know is what to do to launch the URL in order to update my database.
Did I correctly realize that you need to execute some POST-request with some parameters and you need to execute it without firing a browser?
Since your are not actually trying to display a web page, you can parse an HTTP response in many ways. Here is an example using a JSON string:
How do I parse JSON from a Java HTTPResponse?
Basically, this is the important part:
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
The server response is essentially an input file. If it's HTML, you can use JSOUP or some other HTML parser.
You should be cautious of a few things. First, you cannot do this on the UI thread. Second, the user may close the app before it's processed so you need UI progress bars or maybe you don't care...
Last, you probably need to check for network status, etc., before making the call. WebView fails gracefully, but your app may not.
Related
I'm trying to make a little utility that will synchronise data between two servers. Most of the calls there are REST calls with JSON, so I decided to use Apache HttpClient for this.
There is however a section where I need to upload a file. I'm trying to do this using the mutipart form data with the MutipartEntityBuilder but I encounter a Content too long problem. (I tried to gzip the contents of the file too, but I'm still going over the limit).
Here's my java code:
HttpPost request = new HttpPost(baseUrl+URL);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//create upload file params
builder.addTextBody("scanName", "Test upload");
builder.addBinaryBody("myfile", f);
HttpEntity params= builder.build();
request.setEntity(params);
request.addHeader("content-type","multipart/form-data");
HttpResponse response = httpClient.execute(request);
Are there better atlernatives that I should be using for the file upload part? I'm also going to download the files from one of the server. Will I hit a similar issue when try to handle those responses?
Is there something I'm doing wrong?
I try to use your code and send some file with size something about 33MB and it was successful. So, I think your problem one of the follows:
Created http client has limitations for request size - in this case you need to change properties of client or use another client;
In some peace of code you call HttpEntity.getContent() method. For multipart request for this method exists limitations - 25kB. For this case you need to use writeTo(OutputStream) instead of getContent()
In comments you told about swagger, but I don't understand what does it mean. If you use swagger generated api, that problems maybe occurred at their code and you need to fix generation logic (or something like this - I never used swagger)
I hope my answer will help you
I am doing this as part of enhancing a Selenium Webdriver script.
I have tried using httpclient with Java and a lot of other things but I am not able to get anywhere.
Please help!
Ths is the scenario:
After a certain action is performed in a webpage like a button click,
GET/POST methods can be seen in the Developer Tools in Chrome.
I have taken the example of Google here.
What I need here is to collect all the resource names until a certain resource appears (If you open the developer tools in Chrome and navigate to google.com , under the Network tab on the leftmost column you will see tia.png , just an example).
There are two things that should be achieved:
ensure that a certain resource was loaded
make sure the page is completely loaded (all GET / POST methods have been transferred) before any other action is taken.
The httpclient, httpurlconnection only capture one request, but a page sends a lot of requests. How do we capture all of them?
By Using http apache client , you can use something like this :-
To solve your problem, you can get the response code "response.getStatusLine().getStatusCode()" and check for the expected response code and can also play with the outcome of the response.
URI url = new URI("String URI");
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
HttpResponse response = HttpClientBuilder.create().build().execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("Ressponse 1 >>>>"+result.toString());
And , if you need to send parameters in your post request like :-
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", "yourusername"));
urlParameters.add(new BasicNameValuePair("password", "yourPassword"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
This is the answer to my question :
https://kenneth.io/blog/2014/12/28/taking-chrome-devtools-outside-the-browser/
https://github.com/auchenberg/chrome-devtools-app
From the blog :
It’s a standalone app that runs Chrome DevTools in its own process.
It’s powered by node-webkit, and it’s able to run on Windows, Mac and
Linux, completely independently of Chrome.
I wanted to capture all the requests (as seen in the screenshot) between the application and the server so that I could ensure all the critical resources, requests have been transferred to and fro and a page has been completely loaded.
I was unable to do this, perform programmatically what we see in the Chrome Dev tool under the Network tab in the image here.
Kenneth's work let's one achieve this to a degree.
This might be a trivial question but I'm trying to send web request to USPS to get a http post response (or email response depending on my request) containing the tracking information based on the tracking number that I send in. The documentation says the xml needs to appended as part of the url like below
http://secure.shippingapis.com/ShippingAPITest.dll?API=TrackV2&XML=<PTSEmailRequest USERID="xxxxx"><TrackId>xxxxx</TrackId><RequestType>EN</RequestType></PTSEmailRequest>
I saw there were 2 ways to make an xml request, one using HttpPost and the other URLConnection. I'm a bit thrown by how I go about this and I'm failing to appreciate what's the difference between appending xml in the url and a normal http request. Can someone please clear things up for me?
USPS documentation for tracking =>
https://www.usps.com/business/web-tools-apis/track-and-confirm.pdf
I read these related Stackoverflow posts
Java: How to send a XML request?
posting XML request in java
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://secure.shippingapis.com/ShippingAPITest.dll");
List<String> params = new ArrayList<String>(2);
params.add(new BasicNameValuePair("API", "TrackV2"));
params.add(new BasicNameValuePair("XML", FuncTOGenerateXML()));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
//.....
// .....
instream.close();
}
An HTTP request can use one of several methods, like POST, GET, DELETE, PUT... Here we talk about POST and GET
Technical differences
With GET, the data is retrieved from the parameters in the URL.
With POST, the data is retrieved from the data transmitted inside the HTTP message.
Intended use differences
GET is intended to be used when the request does not cause a change (v.g., searching in Google). Since you can repeat the request without side effects, the data is in the URL and can be stored in the browser history, favorites, etc.
POST is intended to use when you are performing a change (v.g. sending an e-mail, doing a on-line purchase). The data related is not stored with the URL (it is then that, if you go back to a page that was obtained using POST, the browser many times will show you a pop-up asking for permission to send the data again.
In real usage, the distinction is not so clear cut, in particular POST is sometimes used when the data is too large (URLs have limited length). Also, sometimes GET is used with the meaning of POST so the data can be presented as an HTML link.
Finally, URLConnection is the basic API for opening a connection (which you can use as a POST or GET request, based in how you pass the data, or something else) and HttpPost is just a higher level API for creating a POST request. If you go the basic way, use HttpURLConnection better.
We have the following code, which later on replaced with HttpHead method as we only need to pull back the header info of our web pages. After the change, we noticed that, on average, it took longer time for the HttpHead to return than the HttpGet for same sets of webpages. Is it normal? What could be wrong here?
HttpClient httpclient = new DefaultHttpClient();
// the time it takes to open TCP connection.
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, this.timeout);
// timeout when server does not send data.
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, this.timeout);
// the get method
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httphead);
Is it normal?
It certainly seems a bit peculiar.
What could be wrong here?
It is difficult to say. It would seem that the strange behavior is most likely on the server side. I would check the following:
Write a micro-benchmark that repeatedly GETs and HEADs the same page to make sure that the performance difference is real, and not an artifact of the way you measured it.
Use packet logger to look at what is actually being sent and received.
Check the server logs.
Profile the server code under load using your micro-benchmark.
One possible explanation is that the HEAD is loading data from a (slow) database or file system. The following GET could then be faster because the data has already been cached. (It could be explicit caching in the server code, the query caching in the back-end database, or file system caching.) You could test for this by seeing if a GET is slower if not preceded by a HEAD.
I have a web service that I built... what I am trying to do now is send a simple request that contains a json query string from a Tapestry web app to that web service. I searched around and most people say to use Apache HttpClient to achieve this. Along with HttpClient I am using URIBuilder.
The Json object that I am trying to send looks like this
{"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
*I realize the issues with the password being sent in plain text etc...
The url that works (tested by manually entering in a web browser and this web service already services an Android client and an iOS client) looks like this
http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"}
Here is the HttpClient code that I have mashed together from google'ing around trying to figure out why this wont work. Essentially what I am trying to do is create a URI with URIBuilder and then construct an HttpPost or HttpGet object with the newly built URI. But something is going wrong in the URIBuilding process. When I debug, an exception gets thrown when I try to set all the aspects of the URI.
Object onSuccess() throws ClientProtocolException, IOException, URISyntaxException{
// json = {"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
String json = user.toJson();
URIBuilder builder = new URIBuilder();
// Error gets thrown when I step over the next line
builder.setScheme("http").setHost("##.###.##.###").setPort(8080).setPath("createuser").setQuery("json=" +json);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
DefaultHttpClient httpClient = new DefaultHttpClient();
String tmp = request.getURI().toString();
HttpResponse response = httpClient.execute(request);
index.setResponse(EntityUtils.toString(response.getEntity()));
return index;
The error that comes back when I step over the line that I commented in the code is
[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
I have tried a lot of other combinations of methods and objects to get this request to send off to the server correctly and nothing seems to work. Hopefully I am overlooking something relatively simple.
Thanks in advance for any guidance you can provide.
You most likely have the wrong version or two versions of the apache httpcomponents on your classpath. If you are running Tapestry it will print out all packages on the classpath on the error page. Investigate there, find which httpcomponents is loaded, figure out where it comes from and fix it.
If this does not work, you should share some of your runtime environment with us. Which servlet engine, running from which IDE or are you running from the command line. Are you using Maven? If so share your pom. Etc.