使用 Android ksoap2,我正在尝试与示例 .NET Web 服务进行通信。我不断收到 http 响应代码 415。我已经尝试了很多东西,很多来自这个网站,如果有任何想法,我将不胜感激..
服务.cs:
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
IService.cs:
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
安卓:
try
{
SoapPrimitive response;
SoapObject request = new SoapObject("http://tempuri.org/", "GetData");
request.addProperty("value", 0);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://www.testsite.com/servicetest/Service.svc");
androidHttpTransport.call("http://tempuri.org/GetData", envelope);
response=(SoapPrimitive)envelope.getResponse();
}
catch(Exception e)
{
Log.e("Exception:",e.getMessage());
}
异常总是发生在 androidHttpTransport.call 行,并显示“HTTP 请求失败,HTTP 状态:415。调试 Web 服务,我可以看到它在 localhost 和http://www.testsite.com/servicetest/上工作,但在从 Android 应用程序调用时却不行。有任何想法吗?