我正在尝试通过 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 方法。我正在尝试使用下面的代码。但没有用。帮助我。提前谢谢。
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";
}