i need to pass a string of arabic value to the HttpURL as a parameter but before i do say when i print the message its showing only question marks
public void sendSms(SendSms object) throws MalformedURLException,ProtocolException,IOException {
String message=new String(object.getMessage().getBytes(), "UTF-8");
System.out.println(message);//printing only question marks
}
and even when i send the message as url parameter its not sending the original message as arabic its sending the question marks.
public void sendSms(SendSms object) throws MalformedURLException, ProtocolException,IOException {
String message=new String(object.getMessage().getBytes(), "UTF-8");
System.out.println(message);
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print(message);
String charset="UTF-8";
URL url = new URL("http://62.215.226.164/fccsms_P.aspx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept-Charset", charset);
con.setRequestMethod("POST");
//con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en,ar_KW;q=0.5");
con.setRequestProperty("Content-Type", "text/html;charset=utf-8");
String urlParameters = "UID=test&P=test&S=InfoText&G=965"+object.getPhone()+"&M= Hello "+object.getName()+" "+message+" &L=A";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
At the very least, you need to follow the advice in erickson’s answer. If there is still a problem, read on…
A Windows command window is pretty limited in what characters it can display. I don’t know how various language packs and code pages affect it, but I know an English edition of Windows will display Arabic characters (and most characters above U+00FF) as
?
.So it’s entirely possible that your characters are correct, they are just being shown as
?
when printed.Instead of using System.out.println, use a Logger. Then you can add a Handler to your Logger (or the root Logger) which writes to a file, and you can examine that file:
(By the way, MalformedURLException and ProtocolException are both subclasses of IOException, so you only need
throws IOException
in your method signature. It inherently covers the other two exceptions.)In some other class, presumably in your
main
method:Assuming that
SendSms.getMessage()
returns aString
, what did you intend with this line?Encoding and decoding the message is a waste at best—if it works, you will just get back the string you started with. And it will only work if the default encoding is UTF-8. In this case, it's corrupting the message.
When you encode a
String
withgetBytes()
, the platform default encoding is used. Unless the system's native character set supports Arabic, any Arabic character will be replaced with a supported character, usually?
.Try this instead: