0

I've created an apache server that I'm hosting on a Linux computer. On calling the URL it should return a JSON value. When I run the below code with my IP instead of the hosted webpage it returns a JSON value but when I put my hosted link URL (hosted with NO-IP) it returns the HTML that's behind the site. I don't know why I'm not getting JSON when I use the hosted URL instead of IP. Any suggestions?

import java.net. * ;
import java.io. * ;

public class UrlTest {

    public static void main(String ar[]) {
        try {
            //below returns HTML
            URL server = new URL("http://servername.ddns.net/test.php");
            //below returns JSON val
            //URL server = new URL("http://ip/test.php"); 
            BufferedReader in =new BufferedReader(new InputStreamReader(server.openStream()));

            String l;
            while ((l = in.readLine()) != null)
                  System.out.println(l);
            in.close();
        } catch(Exception ex) {
        }
    }
}

Snippet of output with servername.ddns.net/test.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<!-- WEB REDIRECTION PROVIDED BY NOIP.COM  http://www.noip.com/ -->
<head>
<TITLE>Jaison Server</TITLE>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="icon" href="http://ip//favicon.ico" type="image/ico">
<link rel="shortcut icon" href="http://ip//favicon.ico">
</head>
<!--
<script language="JavaScript">
if(window != top) {
        top.location.href = location.href;
}
</script>
--> 

Expected output (also the output I get when I give the server IP):

{"chats":[{"text":"test text 1","sender":"Jaison Thomas"},{"text":"test text2","sender":"Sania Ejaz"}]}

Regions of code and output which had my IP or server URL have been replaced for security reasons.

as requested server code

<?php

$servername = "localhost";
$username = "jaison";
$password = "123456";
$dbname = "Chat";



$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT * FROM Text";
$result = $conn->query($sql);

$arraySQL = array();
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    $arraySQL['chats'][] = $row;
  }
  echo json_encode($arraySQL);
} else {
  echo "0 results";
}
$conn->close();
?>
Jaison Thomas
  • 393
  • 1
  • 9
  • 1
    what is the HTTP status of the response? I think it will be other than 200 – Nitin Dec 15 '20 at 16:26
  • Is your domain properly setup? Is it pointing to the correct port? Has it occurred to you that you might need set the `Accept` header ([example](https://pastebin.com/Tx9YhnkR))? If all else fails, then there is a problem with your server, not your client. – Cardinal System Dec 15 '20 at 16:38
  • do uk a code snippet i can test it with.? went thro the html it doesn't have a status code. It is connecting and pulling the page tho cause it gets the title of the page as u can see in the output. – Jaison Thomas Dec 15 '20 at 16:39
  • @Cardinal-ReinstateMonica this looks like it will work im going to give it a try – Jaison Thomas Dec 15 '20 at 16:41
  • @Cardinal-ReinstateMonica i tried this didn't work getting html again for some reason. IK the porting works cause when i run the url in my browser i get the json value. – Jaison Thomas Dec 15 '20 at 16:45
  • You are going to have to show us your server code. There is clearly a problem on the back-end. – Cardinal System Dec 15 '20 at 16:46
  • @JaisonThomas why would the status code be in the HTML? Look at this: https://stackoverflow.com/a/6467904/5645656 – Cardinal System Dec 15 '20 at 16:48
  • @Cardinal-ReinstateMonica again it works when i send the ip of the server just the hosted adress doesnt work but ill attach my php file. The server is an apache server i didnt change the working to to it just added php files will edit main question please check it in a few mins. – Jaison Thomas Dec 15 '20 at 16:48
  • @Cardinal-ReinstateMonica i tried the code to get status i'm getting a response of 200. – Jaison Thomas Dec 15 '20 at 16:50
  • @Cardinal-ReinstateMonica added the server side script – Jaison Thomas Dec 15 '20 at 17:05
  • 1
    The HTML you refer is a redirection, your browser will do the redirection but the Java not. I think that the issue comes from a DNS configuration of servername.ddns.net. In the comment I see "WEB Rediction provided by NOIP.com" – Vincent Lochen Dec 15 '20 at 17:11
  • @VincentLochen is there a way to do the redirection in java or set up the NO-IP hostname to directly send values instead of redirecting? – Jaison Thomas Dec 15 '20 at 17:14
  • 1
    @VincentLochen is correct (I should have looked at the HTML). You can [follow redirects using HttpClient](https://stackoverflow.com/a/23181680/5645656). – Cardinal System Dec 15 '20 at 18:09

0 Answers0