Selenium Script To Wrap The calls Java - 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

Related

read url content WHILE it is continuously updating

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.

How to send strings to server using Java?

I need to send a lot of strings to a web server using Java.
I have a List<String> with huge amount of strings and I need to send it via POST request to the Struts2 action on the server side.
I have tried something starting with
HttpPost httppost = new HttpPost(urlStr);
but don't know how to use it.
On other side I have a Struts2 action, and getting the POST request is easy to me.
I think this solution is too close, but it doesn't solve my problem because it's using just one string :
HTTP POST using JSON in Java
So, how to send many strings to a server using Java?
You should do somthing
HttpPost httppost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<>();
for(String s : list)
params.add(new BasicNameValuePair("param", s));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
on the other side is an action mapped to the url has setter for param. It should be
List<String> or String[]. The action when intercepted will populate that param property.

UrlEncodedFormEntity doesn't encode underscore

I want to use a remote API from my Android device, but for some reason, the UrlEncodedFormEntity class doesn't transform the _ with %5f like the remote API seems to expect. As a consequence, using this code:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(
new BasicNameValuePair("json",
"{\"params\":{\"player_name\":\"Toto\",
\"password\":\"clearPass\"},
\"class_name\":\"ApiMasterAuthentication\",
\"method_name\":\"login\"}")
);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
ResponseHandler responseHandler = new BasicResponseHandler();
httpClient.execute(httpPost, responseHandler);
send a post request to the server, with this content:
json=%7B%22params%22%3A%7B%22player_name%22%3A%22Toto%22%2C%22password%22%3A%22clearPass%22%7D%2C%22class_name%22%3A%22ApiMasterAuthentication%22%2C%22method_name%22%3A%22login%22%7D
I would like it to be like this (replacing the preivous underscore by %5F):
json=%7B%22params%22%3A%7B%22player%5Fname%22%3A%22Toto%22%2C%22password%22%3A%22clearPass%22%7D%2C%22class%5Fname%22%3A%22ApiMasterAuthentication%22%2C%22method%5Fname%22%3A%22login%22%7D
I don't have control over the API, and the official client of the API behave like this. It seems to be the expected behaviour for an URL normalization
Am I missing something? I first thought it was an UTF-8 encoding issue, but adding HTTP.UTF-8 in the constructor of UrlEncodedFormEntity doesn't solve the problem.
Thanks for your help.
EDIT: Finally, the problem didn't come from this unescape underscore. Even if the other client I tried to reproduce the behaviour escaped it, I only had to set the proper header:
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
And the request worked just fine. Thanks everyone, and especially singh.jagmohan for his help (even if the problem was finally elsewhere)!
"_" isn't a reserved symbol for urls.
setting : Content-Type: application/x-www-form-urlencoded'
should solve the problem. Otherwise you can try replacing it, if you really need this option:
String.Replace("_", "%5f");
See percent encodeing , replace
You can try the following code, it works for me.
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
MultipartEntity multipartEntity = new MultipartEntity();
// Also, in place of building JSON string as below, you can build a **JSONObject**
// and then use jsonObject.toString() while building the **StringBody** object
String requestJsonStr = "{\"params\":{\"player_name\":\"Toto\",\"password\":\"clearPass\"},\"class_name\":\"ApiMasterAuthentication\",\"method_name\":\"login\"}";
multipartEntity.addPart("json", new StringBody(requestJsonStr));
httpPost.setEntity(multipartEntity);
HttpResponse response = httpClient.execute(httpPost);
} catch (Exception ex) {
// add specific exception catch block above
// I have used this one just for code snippet
}
PS: The code snippet requires two jar files apache-mime4j-0.6.jar and httpmime-4.0.1.jar.
Hope this helps.

Perform HTTP DELETE with api address and json data using java

I know that to send a POST request to the web I can use this syntax:
HttpPost post = new HttpPost(api_address);
String response = null;
int status_code = -1;
StringEntity se = new StringEntity(json_data, HTTP.UTF_8);
se.setContentType("application/json");
// Set entity
post.setEntity(se);
However, the setEntity methos does not exist for DELETE. So what are the alternatives to send a DELETE with data?
I gave a look to this: HttpDelete with body
but I didnt understand it really... I'm just a beginner!
You can use the solution provided in HttpDelete with body like this:
HttpDeleteWithBody delete = new HttpDeleteWithBody(api_address);
StringEntity se = new StringEntity(json_data, HTTP.UTF_8);
se.setContentType("application/json");
delete.setEntity(se);
This works for me.
But the code listed in
HttpDelete with body
is using annotation library so removed below portion if you do not wanted to include annotation jars else it is ok.
Import: import org.apache.http.annotation.NotThreadSafe;
Annotation above the class:#NotThreadSafe
and place the class in the application and use it according to "fiddler's" comment.
I am sure you can have result.As i am getting success.

Wrapper class for HttpGet / Post in Java?

Sorry, I'm quite new to Java.
I've stumbled across HttpGet and HttpPost which seem to be perfect for my needs, but a little long winded. I have written a rather bad wrapper class, but does anyone know of where to get a better one?
Ideally, I'd be able to do
String response = fetchContent("http://url/", postdata);
where postdata is optional.
Thanks!
HttpClient sounds like what you want. You certainly can't do stuff like the above in one line, but it's a fully-fledged HTTP library that wraps up Get/Post requests (and the rest).
I would consider using the HttpClient library. From their documentation, you can generate a POST like this:
PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.
There are a number of advanced options for configuring the client should you eventually required those.

Categories