简体   繁体   中英

C# Send unicode SMS using UTF8 encoding

I am using http api to send sms. I have a following php code to send unicode sms

<?php 
$api_key = 'XXXXXX';
 $contacts = '97656XXXXX,98012XXXXX';
 $from = 'DEMO';
 $sms_text = 'Hindi - हिंदी , Chinese - 痴呢色 ,Russian - руссиан';
 $encoded_text = utf8_encode($sms_text);
 $message = urlencode($encoded_text);

 $api_url = "http://www.example.in/app/smsapi/index.php?key=".$api_key."&campaign=1&type=unicode&contacts=".$contacts."&senderid=".$from."&msg=".$message; 

//Submit to server

$response = file_get_contents( $api_url);
 echo $response; 
?>

Php code send message correctly. Here now I want to Send the same using c# . I tried following code.

WebClient objClient = new WebClient();
string baseurl = SystemConfiguration.GetSendSMSURL();            
byte[] utfByte = Encoding.UTF8.GetBytes(message);
baseurl = string.Format(baseurl, recipientMobileNumber, HttpUtility.UrlEncode(utfByte));
Stream data = objClient.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();

But it is not working properly. I am receiving ????? in sms at Unicode character. Basically I am trying to convert php code in c#. Can someone tell me where I am doing wrong in c#?

I suppose

  • the php code sends the message correctly
  • the type=unicode is also included in the SMS URL in C#. (It is not present in your code sample.)

If all this is true, I suggest you try to use HttpUtility.UrlEncode directly on the message instead of converting it to byte[] array first. string is already unicode in C# .NET.

baseurl = string.Format(baseurl, recipientMobileNumber, HttpUtility.UrlEncode(message));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM