2012-04-02 73 views
0

我想通过Get方法使用HTTP客户端向Kannel发送短信: - 我不想编码它;因为@字符无法正确发送。如何使用HTTP客户端将@字符发送给Kannel?

如果我没有编码发送,我得到空间字符的错误。 的代码如下:

StringBuffer directives=new StringBuffer(); 
    HttpClient client = new DefaultHttpClient(); 
    directives.append("username=" + username); 
    directives.append("&password=" + password); 
    directives.append("&from=" + from); 
    directives.append("&to=" + to); 
    directives.append("&coding=0"); 
    // Without Encoding 
    directives.append("&text=" + text); 
    directives.append("&smsc=" + smsc); 
    directives.append("&dlr-mask=" + dlrmask); 
    directives.append("&dlr-url=" + dlrurl); 
    URI uri = URIUtils.createURI("http", host, Integer.parseInt(port), "/cgi-bin/sendsms", directives.toString(), null); 
    HttpGet httpget = new HttpGet(uri); 
    HttpResponse httpResponse = client.execute(httpget); 

我试图使用它的POST方法,其中,I设置内容类型= text/plain的发送: - Kannel的未接受所述参数

以下是代码:

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://" + host + ":" + port + "/cgi-bin/sendsms"); 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("username", username)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 
nameValuePairs.add(new BasicNameValuePair("from", from)); 
nameValuePairs.add(new BasicNameValuePair("to",to)); 
nameValuePairs.add(new BasicNameValuePair("text",text)); 
nameValuePairs.add(new BasicNameValuePair("smsc", smsc)); 
nameValuePairs.add(new BasicNameValuePair("dlr-mask",dlrmask)); 
nameValuePairs.add(new BasicNameValuePair("dlr-url",dlrurl)); 
post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse httpResponse = client.execute(post); 

那么什么是最好的方式发送短信到kannel?

谢谢。

+0

你有没有尝试添加“charset = ...”参数与您通过HTTP发送的字符串字符集?例如“&charset = utf-8”。 – 2012-04-03 06:28:57

+0

我尝试过使用“&charset = utf-8”。它不工作。它给空间字符的错误。 – varsha 2012-04-03 06:45:55

+0

“text”参数必须是URL编码的,空格应该表示为“+”或“%20”。 – 2012-04-03 08:09:03

回答

0

您需要配置的Kannel与post

用户组工作

group = sms-service 

更换get-urlpost-url

您还需要以下参数仅与邮政

send-sender 
strip-keyword 
工作

在您的应用程序,您还需要有一些自定义页眉使用Kannel POST

这头可能包括

X-Kannel-From Only sent if send-sender is true 
X-Kannel-To 
X-Kannel-Time  
X-Kannel-UDH in hex format: 06050415820000 
X-Kannel-SMSC  
X-Kannel-MClass 
X-Kannel-PID  
X-Kannel-Alt-DCS  
X-Kannel-MWI  
X-Kannel-Coding 0=7 Bits, 1=8 Bits, 2=UCS-2 
X-Kannel-Compress  
X-Kannel-Validity  
X-Kannel-Deferred  
X-Kannel-Service 

请参阅Kannel Post文档http://www.kannel.org/download/1.4.3/userguide-1.4.3/userguide.html了解它的工作方式

相关问题