2010-06-02 49 views
1

毫無例外工作時,此代碼的工作,但POST請求不起作用。我做錯了什麼?我使用Java 1.6,JBoss 4.2.3HTTP POST與基本授權沒有在Java中

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(); 

回答

3
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();