如何使用HTTP客户端将@字符发送到Kannel?

时间:2012-04-02 14:27:47

标签: httpclient kannel

我尝试使用HTTP客户端通过Get方法向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方法发送它,我在其中设置内容类型= 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的最佳方式是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

您需要配置kannel才能使用post

用户组

 group = sms-service

get-url替换为post-url

您还需要以下仅适用于Post

的参数
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以了解其工作方式