Getting stuck in an infinite-loop while retrieving api result.
Initialy I am getting scan percentage for 2-3 times but after that I am not getting any response.
My java code:
int responseCode=200;
String responseText = "text";
while (!responseText.equalsIgnoreCase("100") && responseCode == 200) {
URL urlForGetRequest = new URL("http://localhost:8090/burp/scanner/status");
String readLine = null;
HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
conection.setRequestMethod("GET");
responseCode = conection.getResponseCode();
System.out.println("response" + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream()));
String response = "";
while ((readLine = in.readLine()) != null) {
response += (readLine);
}
in.close();
JSONObject jsonObj = new JSONObject(response);
// print result
responseText = String.valueOf(jsonObj.get("scanPercentage"));
System.out.println(responseText);
TimeUnit.MINUTES.sleep(2);
}
output I got
response200
0
response200
4
response200
14
response200
17
and after this code kept on running without any output
Note: I perform get from its swagger UI,there is 1 error. i.e TypeError: Failed to fetch
There may be a chance the issue is in the response text check the actual text what the sender is sending
or just eliminate the extra space from the response text using following oneline code before processing the response text. It may be work for you if such problem is there..
responseText=responseText.replace(/^\s+|\s+$/g, '');
Put the code in try, catch block and check what exception it's throwing.
You could try to check if it gets stuck in the while loop, by adding an output and see if it keeps writing when it gets stuck.
while ((readLine = in.readLine()) != null) {
response += (readLine);
System.out.print(".");
}
I don't think this is the reason but it is worth a try.
Related
I have a simple program that sends a GET request and fetches a string.
The URL is a PHP page I wrote that fetches some data from a database on the server and parses it into a string with a bunch of white-space characters (which I need).
The problem is the spaces get omitted in the response to the java app, and my question is how to avoid omitting them?
My java code:
URL servicesUrlObj = new URL("https://myurl.mysite.com/placeholder.php");
HttpURLConnection connection = (HttpURLConnection) servicesUrlObj.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + servicesUrlObj);
System.out.println("Response Code : " + responseCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
content.append(inputLine);
}
reader.close();
System.out.println(content.toString());
original string:
200329951 123 3 03/09/18
the string that gets returned from the request:
200329951123303/09/18
It's not a very elegant solution, but you could consider adding a content.append(" ") to your while loop, or even just turning your current line into content.append(inputLine + " ");
Thanks to Manu Rivas's comment, i found the problem. I wasn't appending the spaces to the output parameter in my php file correctly.
I did:
$spaces . ' ';
instead of:
$spaces = $spaces . ' ';
I am pretty new to Java, and I couldn't find a proper answer to this anywhere, so:
I'm trying to check if the loaded URL can't load because of an error or more specifically error 429 (Too many requests).
I am getting the data from the URL using this:
String finalLine = null;
URL oracle = new URL(url);
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
finalLine = inputLine;
in.close();
return finalLine;
It works very well actually, after that I'm using GSON to parse it.
When it doesn't work the game I'm working on is just crashing. So I would like to code an if function or something that handles the error and does stuff accordingly.
Is there a proper way of doing that? thanks!
If this is what you mean, you can try checking the response code using HttpURLConnection#getResponseCode().
URL oracle = new URL("http://httpstat.us/429"); // url whose response code is 429
HttpURLConnection openConnection = (HttpURLConnection) oracle.openConnection();
if (openConnection.getResponseCode() == 429) {
System.out.println("Too many request");
} else {
System.out.println("ok");
}
openConnection.disconnect();
I write a program to capture the JSON response from the server which contain some needed information I needed. I discovered that sometime my program will not able to capture the correct JSON string and sometime it's works well with no problem. I try to check my code for capturing the response and have no idea on it. When I check the JSON string from server, it's contain the field I want but my program not able to capture the correct data.
This is my JSON String
"info":{
"reason":"Fine",
"boolean":false,
"post":{
"actions":"",
"actions_In_process":"Checked",
"destination":"C%3ApdfFdoc%20edipdfFdestinationpdfFexample.pdf",
"file_type":"pdf",
},
This is my program for capture the JSON string and the field I need is action_In_process
String Url1 = "http://IP:port/etc/";
HttpURLConnection con = (HttpURLConnection) Url1.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if(responseCode == 200)
{
try
{
InputStream is = con.getInputStream();
BufferedReader read = new BufferedReader (new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String data = "" ;
while((data = read.readLine() ) != null )
{
buffer.append(data);
}
String JsonData = buffer.toString();
JSONObject jobj = new JSONObject(JsonData);
JSONObject process_info = jobj.getJSONObject("info");
JSONObject pi = process_info.getJSONObject("post");
String action_run = pi.getString("actions_In_process");
System.out.println("Process:" +action_run);
What I had found out is sometime the Process showing is blank but when I get back the JSON data and I found out the field I need is inside the JSON response. Please share your opinion on this issues
This is the message showing my compiler if I not able to capture the correct JSON string
Process :
If in normal condition
Process : check
BufferedReader's readline() is blocking.
I'm trying to read a json by using jersey api. That's a long json.
The problem is I can't get the full json. Please look at both links below for more understanding.
Expect result. (10 objects)
Actual result (Sorry because this one can't be format. But it can only get 4 objects and a bit of the 5th object.)
This is my code to get json:
Client client = Client.create();
WebResource resource = client.resource(url);
ClientResponse clientResponse = resource.accept("application/json").get(ClientResponse.class);
if(clientResponse.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + clientResponse.getStatus());
}
String output = "";
String response = "";
BufferedReader br = new BufferedReader(new InputStreamReader(
(clientResponse.getEntityInputStream())));
while ((output = br.readLine()) != null) {
response = response + output;
}
br.close();
return response;
I don't know what I did wrong here.
Your client is receiving the full output. You are seeing the truncated output in the log because the LoggingFilter that you have enabled by default will truncate the output.
Check the constructors here for how to set the maxEntitySize.
https://jersey.java.net/apidocs/2.11/jersey/org/glassfish/jersey/filter/LoggingFilter.html
I am using a PHP script to access my database through my java program. PHP script sends and email and prints sent if the script was run successfully and the email was sent. The java program uses :
InputStream in = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpPost);
in = response.getEntity().getContent();
String inn = in.toString();
} catch (IOException e) {
}
String result = null;
String sent = "sent";
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
in.close();
result = sb.toString();
in order to get whats printed from the PHP script. Using Log i know that "sent" is being returned and the variable 'result' has the value "sent" when the PHP script successfully runs.
Now in the java file i am trying to do the following:
Log.e("result",result); <-- returns sent
if(result.equals("sent"))
{
... do something
}
I have tried a few different ways but no matter which way i try to compare 'result' to 'sent' it always fails.
If anyone has an idea of why this is happening I'd greatly appreciate any advice. Im sure its silly but i assume it has to do with PHP not returning exactly what i think it is, even tho Log shows that it is.
Maybe try:
result.toString().equalsIgnoreCase("sent")
You could also do:
Log.e("result",result);
System.out.println( "!" + result.toString() + "!" );
if(result.equals("sent"))
{
System.out.println( "Success" );
}