Android将对象发送到带有标头的c#web服务

时间:2016-08-31 14:10:36

标签: c# android http-headers http-post http-get

我有一个接收SoapHeader类对象的Web服务,Web服务的代码在下面

public class MyHeader : SoapHeader
{
     public DateTime Created;
     public string Username{get; set;}
     public long Expires { get; set; }
}

[WebMethod(EnableSession = true)]
[SoapHeader("myHeaderMemberVariable")]
public void MyWebMethod()
{
     // Process the SoapHeader.
     if (myHeaderMemberVariable.Username.Equals("admin"))
     {
          // Do something interesting.
     }
}

我需要从我的Android应用程序调用此Web服务,我使用以下代码:

String body ="admin";
StringEntity stringEntity = new StringEntity(body, "UTF-8");
stringEntity.setChunked(true);

// Request parameters and other properties.
HttpPost httpPost = new HttpPost("http://10.2.2.184/service/servicedata.asmx?MyWebMethod");
httpPost.setEntity(stringEntity);
httpPost.addHeader("Accept", "text/xml");
httpPost.addHeader("myHeaderMemberVariable", soapAction);

// Execute and get the response.
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;

response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity();

String strResponse = null;
if (entity != null) {
        strResponse = EntityUtils.toString(entity);
}
} catch (IOException e) {
        e.printStackTrace();
}

问题是我在使用HttpGet.setHeader时无法指定对象的字段,因为它接收字符串值。那么如何指定'用户名'的值?和'到期'我的android代码中的SoapHeader对象。

0 个答案:

没有答案