Code sends SMS working fine on VB but not on this C# code

Tags: c# vb.net
Question!

I have this code in Visual Studio (C#) and the returnmessage is always an empty string even though the API call was successful (the SMS message was delivered successfully). Similar code on VB.net is working fine.

using (System.Net.WebClient client = new System.Net.WebClient())
{
    string url = "http://tekegy.org/?Username=****&Password=****&Sender=***&Recipients=*****&MessageData=Test";
    string returnmessage = client.DownloadString(url);
    Console.WriteLine(returnmessage);
    Console.ReadKey();
}


Answers

I think your server requires a valid http header 'user-agent'. Try:

using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
    string response = client.DownloadString("http://tekegy.org/?Username=****&Password=****&Sender=***&Recipients=*****&MessageData=Test");
    Console.WriteLine(returnmessage);
    Console.ReadKey();
}
By : Veener


I already came to a conclusion myself after looking exhaustively for other related questions to mine in this site. This is the one question that solved it: java.util.NoSuchElementException - Scanner reading user input

When you call, sc.close() in first method, it not only closes your scanner but closes your System.in input stream as well.

As seen in my question, my code instantiated a new scanner inside my method, and after a correct value was entered, the method closes the scanner, which prevents new input to be entered after the method is called subsequently.

This is the revision i made:

public static Integer getValue (Scanner input, int minValue, int maxValue) {
    int value;
    value = 0;
    while (true) {
        try {
            value = input.nextInt();
            if((value < minValue) || (value > maxValue)) {
                // ERROR message goes here
            } else {
                break; // Input successful, break loop.
            }
        } catch (InputMismatchException IME) {
            // ERROR message goes here.
            input.nextLine(); // Discards input so user can try again
        }
    }
    return (value);
}

As seen in this revision, the scanner was previously instantiated in the main function and passed as an argument so it can retrieve input from the user inside the method in subsequent calls.

I also improved the way it receives input taking notes from Deitel's book, Java How To Program Chapter 11: Exception Handling, and I adapted it to my specific case.



You can add (almost) 180 before the modulus, and subtract it later.

Not exactly 180 because then you would get -180 instead of 180.

var val = 0;
for(var i=0; i<8; ++i) {
  console.log(val = (val + 90 + 179) % 360 - 179);
}

By : Oriol


This video can help you solving your question :)
By: admin