我已经通过正常渠道尝试解决此问题,但我似乎无法确定如何将数据正确地从我的 Android 应用程序发送到托管在我的 C# winforms 应用程序中的 WCF 库。
我已经使用 Visual Studio 提供的 WCFClientTest 应用程序检查了该服务是否正确托管,并且一切都在这方面工作。当我调用此行时: androidHttpTransport.call(SOAP_ACTION, envelope); 它似乎挂在 Android 应用程序中
我正在使用 ksoap2 尝试调用 WCF 服务,并使用此链接作为教程。
安卓代码
private static final String METHOD_NAME = "Alive";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ACTION
= "http://tempuri.org/IDeviceHealth/Alive";
//10.0.2.2 is the ip address for the device simulator.
private static final String URL = "http://192.168.1.8:8733/DeviceHealth/HealthService/mex";
//SOAP must be the same version as the webservice.
private static final int SOAP_VERSION = SoapEnvelope.VER12;
public void OnClickGoBtn(View v)
{
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope
= new SoapSerializationEnvelope(SOAP_VERSION);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
String resultData = result.toString();
Log.d("WCF Response", resultData);
}
catch (IOException e)
{
Log.d("WCF IO Error", e.toString());
}
catch (XmlPullParserException e)
{
Log.d("XML Pull Error",e.toString());
}
catch(Exception e)
{
Log.d("General Exception", e.toString());
}
}
C# 服务器:
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="DeviceHealthService.DeviceHealth" behaviorConfiguration="MetadataBehavior">
<endpoint address=""
binding="netTcpBinding"
contract="DeviceHealthService.IDeviceHealth"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:10001"/>
<add baseAddress="http://localhost:10002"/>
</baseAddresses>
</host>
</service>
</services>
public string Alive()
{
return "I am running";
}