Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: If url not valid Return nothing rather return NOT FOUND : How to make that ?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If url not valid Return nothing rather return NOT FOUND : How to make that ?

    Hello
    guys

    I need output return nothing rather than Not Found . Still return ok it the url valid

    How to get that don!!





    Hello
    guys

    I need output return nothing rather than Not Found . Still return ok it the url valid





    package javaUrl;


    import java.net.HttpURLConnection;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.UnknownHostException;


    public class StrTest {

    public static void main(String[] args) {

    int i;


    for(i= 0 ;i<=1000000;i++)

    {
    URLUtils.checkIfURLExists("http://xxxx"+i+".pdf");
    System.out.println();
    System.out.printf("%d ",i+1);
    }



    }

    public static boolean checkIfURLExists(String targetUrl) {
    HttpURLConnection httpUrlConn;
    try {

    httpUrlConn = (HttpURLConnection) new URL(targetUrl)
    .openConnection();



    httpUrlConn.setRequestMethod("HEAD");


    httpUrlConn.setConnectTimeout(30000);
    httpUrlConn.setReadTimeout(30000);

    System.out.println("Response Message: "
    + httpUrlConn.getResponseMessage());

    return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);



    }
    catch (Exception e) {


    return false;
    }
    }

    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: If url not valid Return nothing rather return NOT FOUND : How to make that ?

    Hello.
    If you want to do what you want then just modify the return type of checkIfURLExists() to String rather than boolean.
    In the catch block, write return "";
    In the try block, write return "OK";

    Thanks,
    Syed.

Similar Threads

  1. Return Value Cannot Be Found
    By misscoollike in forum Object Oriented Programming
    Replies: 3
    Last Post: March 12th, 2012, 08:58 PM
  2. How to make a FontChooser return a font in a Notepad.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2012, 10:58 PM
  3. Return variable from an URL.
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 15th, 2011, 07:31 AM
  4. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  5. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 05:27 AM