此代码可以正常工作,但post请求不起作用。我做错了什么?我使用Java1.6,JBoss4.2.3

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<message><service id=\"210\"/><to>+"+phone+"</to>" +
            "<body content-type=\"text/plain\">"+message+"</body></message>";

    String userPassword = "login:password";
    URL url = new URL(ksGateUrl);
    URLConnection urlc =  url.openConnection();

    urlc.setDoOutput(true);
    urlc.setUseCaches(false);
    urlc.setAllowUserInteraction(false);
    urlc.setRequestProperty("Authorization",
            "Basic " + new sun.misc.BASE64Encoder().encode (userPassword.getBytes()));


    OutputStreamWriter wr = new OutputStreamWriter(urlc.getOutputStream(), "UTF-8");
    wr.write(xml);
    wr.flush();

最佳答案

String userPassword = "login:password";
URL url = new URL(ksGateUrl);
// URLConnection urlc =  url.openConnection();
URLConnection urlc =  new URLConnection(url);

urlc.setDoOutput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Authorization",
        "Basic " + new sun.misc.BASE64Encoder().encode (userPassword.getBytes()));

urlc.connect();

10-06 16:00