Please all I've got this API from an SMS reseller here in Nigeria. I want to know how to insert my textbox values corectly
API: http://smsmobile24.com/components/com_spc/smsapi.php?username=xxx&password=yyy&sender=##sender##&recipient=##recipient##&message=##message##
I need to insert password, username and of course sender and recipients.
I have tried doing this:
string to, msg;
to = smsRecipientBox.Text;
msg = smsMsgBox.Text;
http://smsmobile24.com/components/com_spc/smsapi.php?username=C***er&password=fath****am&sender=Cmanager&recipient = '"+to+"'&message='"+msg+"';
But I get that red line telling me there is an error.
You can't modify a constant value (variable that is marked with const modifier).
You have two options here:
1) you remove the const from the field
2) use formatting to insert values in placeholders in the constant string:
const string url = "http://smsmobile24.com/components/com_spc/smsapi.php?username=C***er&password=fath****am&sender=Cmanager&recipient={0}&message={1}";
var to = smsRecipientBox.Text;
var msg = smsMsgBox.Text;
string result = string.Format(url, to, msg);
Related
I have n number of request urls, like below
https://{user.id}/{user.name}/testing1
https://{user.number}/{user.name}/testing1
https://{user.age}/{user.height}/testing1
https://{user.gender}/{user.name}/testing1
https://{user.height}/{user.age}/testing1
https://{user.weight}/{user.number}/testing1
I have the below test data class which has n number of values.
public class User{
public string id = "123";
public string name = "456";
public string age = "789";
public string gender = "1478";
public string height = "5454";
public string weight = "54547";
public string number = "88722";
.......
.......
.......
}
And I need to make the url
https://{user.number}/{user.name}/testing1 into
https://{88722}/{456}/testing1
In my code, I will be randomly getting a request url(from a json file) and i need to replace the parameters with the values given in the class. Can this be done? If yes please help. Thanks
I have tried using string.format() - but it doesnot work, because I am randomly getting the url and I am not sure which value needs to be replaced.
I also tried using the interpolation, but found not helpful either unless i can do something like
User user = new User();
string requesturl = GetRequestJSON(filepath);
//The requesurl variable will have a value like
//"https://{user.id}/{user.name}/testing1";
string afterreplacing = $+requesturl;
Update: After some more googling, I found out that my question is very much similar to this. I can use the first answer's alternate option 2 as a temporary solution.
Unless I am clearly missing the point (and lets assume all the values are in when you do new user(); )
You just want
User user = new User();
string requesturl = $"https://{user.id}/{user.name}/testing1";
then all the variables like {user.id} are replaced with values. such as "https://88722/456/testing1" if you need the {} in there you can add extra {{ to go with such as
string requesturl = $"https://{{{user.id}}}/{{{user.name}}}/testing1";
The SMS Service provider gave a link to send the SMS. this link I put it in my website,But In That Link I have to pass the phone Number and Price dynamically through Label. Can anyone please help Me.
I am Using The Code Which is
var cli = new System.Net.WebClient();
string price = "230";
string number = "9999999999";
cli.DownloadString(#"http://bhashsms.com/api/sendmsg.php?user=ABCXYZ&pass=*****&sender=BSHSMS&phone=9999999999&text=Test SMS YOUR Order HAs Been Placed OF RS. 230 Successfully&priority=ndnd&stype=normal");
Simply put the stuff at the corresponding places in the string to pass to cli.DownloadString(). This examople shows how to do so using string.Format()
var cli = new System.Net.WebClient();
string price = "230";
string number = "9999999999";
var format = #"http://bhashsms.com/api/sendmsg.php?user=ABCXYZ&pass=*****&sender=BSHSMS&phone={0}&text=Test SMS YOUR Order HAs Been Placed OF RS. {1} Successfully&priority=ndnd&stype=normal"
cli.DownloadString(string.Format(format, number, price));
I have the next string:
public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
public const string LoginURL = "{0}sessions";
I want to make a url of LoginURL where the {0} is the BaseAddress.
The logical way is not working:
string str = string.Format(BaseAddress, LoginURL);
It only prints the BaseAddress.
It will work if I do this:
string str = string.Format(LoginURL, BaseAddress); //Is this the correct way?
It's not logical way, you are defining parameter in LoginUrl not in BaseAddress, so it's working correctly
If you want it work logicaly do it that way:
public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/{0}";
public const string LoginURL = "sessions";
string.Format is replacing {0} with first parameter, so it will now work like you want it to work.
string str = string.Format(LoginURL, BaseAddress); //Is this the correct way?
Yes, that is the correct way. Although it doesn't follow the usual pattern people use. Please read the documentation for the function located here: http://msdn.microsoft.com/en-us/library/system.string.format.aspx
The first argument is the Format, the second are the arguments/replacement values. Assuming you want to maintain a constant format, that is usually a hardcoded in the argument as such:
string str = string.Format("{0}sessions", BaseAddress);
Being as it seems the base address is normally more consistent (but may still be variable) an approach such as the following may be better:
public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
public const string LoginURL = "sessions";
string str = string.Format("{0}{1}", BaseAddress, LoginURL);
If your end-goal is just URL combination rather than an exercise using string.Format, the following may still be a better approach:
Uri baseUri = new Uri("http://test.i-swarm.com/i-swarm/api/v1/");
Uri myUri = new Uri(baseUri, "sessions");
That way you don't have to worry about whether or not a separator was used in the base address/relative address/etc.
There is no logical way, there's just one way to comply to how String.Format works. String.Format is defined so that the first parameter contains the string into which parameters will be inserted, then follows a list of the parameters.
If you define the strings like you do, the only correct way is
string str = string.Format(LoginURL, BaseAddress);
It would be more intuitive, however, if the BaseAddress was the format string:
public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/{0}";
public const string LoginURL = "sessions";
So you could write
string str = String.Format(BaseAddress, LoginURL);
Yes the correct way is:
string str = String.Format(LoginURL, BaseAddress);
The format string should always be the first parameter.
Though I feel that:
string str = String.Format("{0}sessions", BaseAddress);
Is more readable.
You can use C#'s interpolated strings.
string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
string LoginURL = $"{BaseAddress}sessions";
You can have multiple parameters. This is a safer method of formatting strings as when using string.Format you need to make sure that order of parameters are correct. With this new way you don't have to.
string foo = "foo";
int bar = 4;
string result = $"{bar} hello {foo}";
Reference:
What's with the dollar sign ($"string")
MSDN - Interpolated Strings
i have already configured SMS gateway & some php code.Now my project is thatone convert it to Csharp with minor changes,
probably i have to write new code.but so.. experienced guys from you i wanted to know is that possible ? Sample code below.
enter code here
$username = "abcdsoft";
$password = "softsoft";
//GET parametrar
//$source = $_GET['sourceaddr'];
//$dest = $_GET['destinationaddr'];
//$message_in = $_GET['message'];
enter code here
$source = $_GET['msisdn'];
$dest = $_GET['shortcode'];
$message_in = $_GET['msg'];
those are most top lines.below code i changed.(php to csharp)
enter code here
string username ='abcdsoft';
string password = 'abcdabcd';
int source = ['sourceaddr'];
int dest = ['shortcode'];
string message_in = ['msg'];
Is this way is correct ?
Almost.
Need to use double-quotes for the string
Request.QueryString is the ASP.NET equivalent to the PHP $_GET.
Also, apparently C# is a bit more strongly typed than PHP, so you need to explicitly convert the query string params to integers.
Code should be like this:
string username = "abcdsoft";
string password = "abcdabcd";
int source = int.Parse(Request.QueryString["sourceaddr"]);
int dest = int.Parse(Request.QueryString["shortcode"]);
string message_in = int.Parse(Request.QueryString["msg"]);
I could use a little help with proper regular expressions to parse the following string into 3 variables. The part with comments saying // TODO: is where I need help with the regular expressions. For now I have just assigned a static value, but need to replace that with real regular expressions that parse the sample text. Thanks!
// This is what a sample text will look like.
var text = "Cashpay #username 55 This is a sample message";
// We need to parse the text into 3 variables.
// 1) username - the user the payment will go to.
// 2) amount - the amount the payment is for.
// 3) message - an optional message for the payment.
var username = "username"; // TODO: Get the username value from the text.
var amount = 55.00; // TODO: Get the amount from the text.
var message = "This is a sample message"; // TODO: Get the message from the text.
// now write out the variables
Console.WriteLine("username: " + username);
Console.WriteLine("amount: " + amount);
Console.WriteLine("message: " + message);
You can use capturing groups:
var regex = new Regex(#"^Cashpay\s+#([A-Za-z0-9_-]+)\s+(\d+)\s+(.+)$");
var text = "Cashpay #username 55 This is a sample message";
var match = regex.Match(text);
if (!match.Success)
//Bad string! Waaaah!
string username = match.Groups[1].Value;
int amount = int.Parse(match.Groups[2].Value);
string message = match.Groups[3].Value;
This method does not do input validation; in some cases this might be ok (e.g. the input is coming from a source which has already been validated). If you are getting this from user input you should probably use a method that is more robust. If it is coming from a trusted source but has multiple formats (e.g. "Cashpay" is one of many choices) you could use a switch or if statement for flow control after the split:
// make sure you validate input (coming from trusted source?)
// before you parse like this.
string list[] = text.Split(new char [] {' '});
if (list[0] == "Cashpay")
{
var username = list[1].SubString(1);
var amount = list[2];
var message = string.Join(' ',list.Skip(3));
}
or
// make sure you validate input (coming from trusted source?)
// before you parse like this.
string list[] = text.Split(new char [] {' '},4);
if (list[0] == "Cashpay")
{
var username = list[1].SubString(1);
var amount = list[2];
var message = list[3];
}