I failed to get facebook recource page using this java code: - java

for a test i want to try get facebook recource page using that java code:
it is work for any other site but facebook\ twitter, any solutions?
public class ReadWebPage {
public static void main(String[] args) {
String urltext = "http://www.facebook.com";
BufferedReader in = null;
try {
URL url = new URL(urltext);
in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

What errors are returned? I suspect it has something to do with "http://www.facebook.com/" redirecting to "https://www.facebook.com". I suggest trying the https address and seeing if that corrects the problem.

Related

Improper_Restriction_of_XXE_REF

I'm new to using the Checkmarx tool and just checking for security flaws in code in general. I have a method which is supposed to read from an input stream. The method works, however I am getting XXE and SSRF errors.
public static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(Normalizer.normalize(line, Normalizer.Form.NFD));
}
} catch (IOException e) {
LOG.error(
"********************",
e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LOG.error(
******************,
e);
}
}
}
return sb.toString();
}

How to extract XML data to a string via URL

I wanna convert xml data from URL to json using this library , but it doesn't handle xml from URLs..! only if it was a string or file, so I wanna convert the data inside the url into a string!
is it possible?
This fragment can help you
new Thread() {
public void run() {
URL url = null;
BufferedReader in = null;
try {
url = new URL("your url");
in = new BufferedReader(
new InputStreamReader(
url.openStream(),"UTF-8"));//in most cases there is utf 8
String inputLine;
StringBuilder builder = new StringBuilder();
while ((inputLine = in.readLine()) != null)
builder.append(inputLine);
String urlContent = builder.toString();
// process your received data somehow
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();

Java connection with Threads

I want to create an App, it can show the newest movies datas, with trailers from Movie Database
I try to connect url, with threads to search the trailers. But its load only the first 38 connect succes, but i need 60. I have no idea why.
Is anyone has?
P.S.: I am sorry my English
private Thread loadTrailers = new Thread(){
#Override
public void run(){
int counter = 0;
String str = new String("");
BufferedReader br = null;
try { // try open source site
URL url = new URL("https://api.themoviedb.org/3/movie/"+id+"/videos?api_key="+myApikey);
br = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream())); // problem with openConnection()
if(br != null){
if((str = br.readLine()) != null){
String[] moviedatas = str.split("\"id\"");
for(int i = 1; i < moviedatas.length && counter < 3;++i){
Pattern isTrailer = Pattern.compile(".*Trailer.*");
Matcher matc_tr = isTrailer.matcher(moviedatas[i]);
if(matc_tr.matches()){
Pattern getKey = Pattern.compile(".*key\":\"(.*)\",\"name.*");
Matcher key = getKey.matcher(moviedatas[i]);
if(key.matches()){
links.add("https://www.youtube.com/watch?v="+key.group(1));
counter++;
}
}
}
}
}
} catch (MalformedURLException e) {
System.err.println("Invalid URL!");
} catch (IOException e) {
System.err.println("Connection failed from trailers.");
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
System.err.println("File close not success.");
}
}
}
};

How to make a relative path to my file?

i use eclipse
and my structure looks like this:
TSG-jar
->src
->myBean.java (here i need a path)
and another one web application
TSG-war
->WebContent
->js
->boot.js
so they are both in the same workspace
I call the boot.js file from TSG-war in my TSG-jar (from myBean.java), and i need a path...but i have no idea how to do it...
In myBean.java i got this code:
private String getSrc(String path) {
try {
BufferedReader in = new BufferedReader(new FileReader(path));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I need the path of the javaScript.js file to send it to the function...
can u help me?
i tried:
String s = "..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
String s = "\\..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
String s = "\\..\\..\\..\\..\\TSG-war\\WebContent\\js\\boot.js"
System.out.println(getSrc(s));
its always null...
where is the error?

How can I call a WebMethod in Java Applet

How can I call a C# WebMethod in my Java Applet ?
The method called EnrollClient in C#
My Try
public void enroll(String teste) {
URL u;
InputStream is = null;
try {
u = new URL("http://localhost:5154/lb.ashx?pwd=abci/EnrollClient");
is = u.openStream();
BufferedReader d = new BufferedReader(new InputStreamReader(is));
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
}
}
C# WebMethod
public class lb : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
strings pwd = context.Request["pwd"].ToString();
business.Client.lb cli = new business.Client.lb();
JavaScriptSerializer jss = new JavaScriptSerializer();
StringBuilder sbRes = new StringBuilder();
jss.Serialize(cli.ReturnJSon(), sbRes);
context.Response.Write(sbRes.ToString());
}
[WebMethod]
public void EnrollClient()
{
string template = string.Empty;
string client = string.Empty;
try
{
business.Client.lb cli = new business.Client.lb();
cli.EnrollClient(template, client);
}
catch (Exception e)
{
}
}
If I do the same code but with the code
u = new URL("http://localhost:5154/lb.ashx?pwd=abci");
it will access the ProcessRequest of my C# code.

Categories

Resources