首页 > 代码库 > 通过wcf的webservice访问服务器

通过wcf的webservice访问服务器

package com.factory.webserviceutils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebServiceUtils {
// 命名空间
public static final String SERVICE_NAMESPACE = "http://tempuri.org/";

public static final String SERVICE_URL = "http://192.168.1.156/ProduceManageSystemWebService/";
/*
* 通过wcf返回服务器数据
* **/
public static List<String> getUser() {
String methodName = "GetManagement";

HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
try {
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE,
methodName);
envelope.bodyOut = soapObject;
envelope.dotNet = true;

ht.call(SERVICE_NAMESPACE + methodName, envelope);
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
return parseUser(detail);

/* String object = result.getProperty(0).toString();
System.out.println("Helloservice::Object" + object); return
object;
*/

}
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

 

public static List<String> parseUser(SoapObject detail) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < detail.getPropertyCount(); i++) {
list.add(detail.getProperty(i).toString().split("=")[1].split(";")[0]);
}
System.out.println("list.size()" + list.size());
for(int i=0;i<list.size();i++)

{
System.out.println("list.get(i)"+list.get(i));
}
return list;

}
}