Web服务方法通过httpconnection调用

时间:2012-11-29 12:48:44

标签: java liferay

我正在尝试通过httpconnection连接webservice方法。这是我的代码

  String data = "- <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org  /soap/envelope/' xmlns:q0='urn:sap-com:document:sap:soap:functions:mc-style' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"
                           + "- <soapenv:Body>"
                           + "- <q0:ZwebserviceTest>"
                           + "<Fromdate>2012-11-27</Fromdate>" 
                             + "<Id>231</Id> "
                             + "<Name>Test User</Name>" 
                             + "<Reason>Persoanl</Reason>" 
                             + "<Todate>2012-11-27</Todate>" 
                             + "</q0:ZwebserviceTest>"
                             + "</soapenv:Body>"
                             + "</soapenv:Envelope>";

这里ZwebserviceTest是webservice方法。我想将这些数据发送到webservice方法。尝试使用下面的代码。但是没有use.help me.thanks提前。

HttpURLConnection conn = (HttpURLConnection) url
             .openConnection(); 

            conn.setRequestMethod("POST");


           BASE64Encoder enc = new sun.misc.BASE64Encoder();
              String userpassword = user + ":" + password;
              String encodedAuthorization = enc.encode( userpassword.getBytes() );
              conn.setRequestProperty("Authorization", "Basic "+
              encodedAuthorization);
            conn.setDoInput(true);
             conn.setDoOutput(true);

             conn.setRequestProperty("SOAPAction",soapact1);

             conn.setRequestMethod("POST");
             conn.setRequestProperty("Content-Length",
               String.valueOf(data.length()));
             conn.setRequestProperty("Content-Type",
               "application/x-www- form-urlencoded");
             conn.setRequestProperty("User-Agent",
               "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
             InputStream is = null;
             OutputStream os = null;
             OutputStreamWriter wr = null;
             try {
              os = conn.getOutputStream();
              System.out.println("os :" + os);
              wr = new OutputStreamWriter(os);
              System.out.println("Data : " + data);
              wr.write(data);
              wr.flush();
              is = conn.getInputStream();

             } catch (IOException e) {

              System.out.println("IO error Response code : "
                + conn.getResponseCode());
              System.out.println("IO error Response Message   : "
                + conn.getResponseMessage());
              System.out.println(" IO error : " + e);
              if ((conn.getResponseMessage() + "")
                .equals("Unauthorized")) {
               System.out.println("on unauthorized");
               //Login.authorized = false;
               //Login.errorMsg = "Wrong User Name or Password";
              }

0 个答案:

没有答案