2

I have a url that sends sms when we run that url in browser. I tried this in java. following is my code:

URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();

I want to run the url without opening the browser. But the problem is i am not reciving any sms( ie code is not working). I sperately tried to run url in browser and i received sms, so no problem with url. Above code is based on some references from internet. Any help is appriciated. Correct me is i am on totally wrong path for solution.

prateek k
  • 137
  • 2
  • 15

3 Answers3

1

The problem with your code is that you never call getInputStream(), getContent() or getHeaderField() which is what will actually launch the request behind the scene.

Try this:

URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
try (InputStream is = myURLConnection.getInputStream()) {}

Or simply:

URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.getContentLength(); // -> calls getHeaderField("content-length")

NB: The 3 methods listed above will automatically call connect() so no need to call it explicitly anymore.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0
public String SendSMS() {
        StringBuilder sURL = new StringBuilder(100);
        StringBuilder sb = new StringBuilder();
        try {
            sURL.append("http://example.com/");
            InputStream is = new URL(sURL.toString()).openStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            }
        } catch (MalformedURLException me) {
            System.out.println("## MalformedURLException occured" + me.getMessage());
        } catch (UnsupportedEncodingException me) {
            System.out.println("## UnsupportedEncodingException occured : " + me.getMessage());
        } catch (IOException me) {
            System.out.println("## IOException occured in : " + me.getMessage());
        } catch (Exception me) {
            System.out.println("## Exception occured : " + me.getMessage());
        }
        return sb1.toString();
    }
Jay Prakash
  • 787
  • 6
  • 22
  • I am new to java. can you please explain ypour code in nutshell? – prateek k Oct 17 '16 at 09:45
  • @prateekk i have written a simple code to connect to a url Method SendSMS will return a string value it will receive form server otherwise it will give all possible exception – Jay Prakash Oct 17 '16 at 09:50
  • @prateekk so you can understand the problem if anything into service or program just copy and paste and sop return value and check – Jay Prakash Oct 17 '16 at 09:51
0

You might want to use the Apache HttpClient Utility.

Add the following maven dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

Next use the following code to hit an URL using HTTP Get method.

//create the url
String url = "http://www.google.com/search?q=httpClient";

//create the http client object 
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
//execute the request and capture the response
HttpResponse response = client.execute(request);

//get response code
System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());
//get the response body
BufferedReader rd = new BufferedReader(
    new InputStreamReader(response.getEntity().getContent()));

//capture the response in string
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}

Added benefit is the HttpClient automatically retries hitting the URL if the URL is down. Also, it provides for retry event handlers.

More Reference: Force retry on specific http status code

Community
  • 1
  • 1
arnabkaycee
  • 1,634
  • 13
  • 26