Web服务\ Android \ Http响应代码415

时间:2014-07-17 11:52:40

标签: android .net web-services ksoap2 android-webservice

使用Android ksoap2,我正在尝试与示例.NET Web服务进行通信。我一直得到http响应代码415.我已经尝试了许多东西,很多来自这个网站,并会感激任何想法..

Service.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应用程序调用时则不行。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在看似永远的事情之后,我发现了一些有效的东西。在Web服务接口中,将其放入ServiceContract和OperationContract中。也可以使用XmlSerializer:

[ServiceContract (Namespace="http://tempuri.org")]
public interface IService
{

    [OperationContract(Action = "http://tempuri.org/GetData")]
    [XmlSerializerFormat(SupportFaults = false)]
    string GetData(int value);

在Android代码中,我使用的是SoapEnvelope.VER12,应该使用SoapEnvelope.VER11。