Like every weekend, today I was spending time on writing application for my MSc thesis. Among few tasks I planned for today one was connected with sending messages/notifications to the internal discussion group of my dormitory using NNTP protocol.

Short research showed that this should be a piece of cake. Below small code snippet showing how to send message using library Apache Commons Net.

public class Main {

 public static void main(String[] args) throws Exception {

 NNTPClient client = new NNTPClient();
 client.connect("news.ustronie.pw.edu.pl");

 client.selectNewsgroup("pw.test");
 Writer postArticle = client.postArticle();

 SimpleNNTPHeader headers =
 new SimpleNNTPHeader("Tomasz Dziurko <tdziurko@gmail.com>", "Test kodowania polskich znaków");
 headers.addNewsgroup("pw.test");
 headers.addHeaderField("Mime-Version", "1.0");
 headers.addHeaderField("Content-Type","text/plain; charset=UTF-8");
 headers.addHeaderField("Content-Transfer-Encoding", "8bit");

 postArticle.write(headers.toString());
 postArticle.write("ąęóśłżźćń - test polskich znaków\r\n");
 postArticle.close();

 client.completePendingCommand();

 client.disconnect();
 }

}

Everything worked as expected until I started to use Polish characters in the message. Then instead of “ąęóśłżźćń” I saw following picture:
Invalid characters encoding in NNTP class in Apache Commons Net Continue reading this post …


Be Sociable, Share!