不向Web服务发送数据

时间:2014-04-24 06:25:33

标签: c# web-services xaml windows-phone-8

这是我发送数据的代码。有一个字符串strJson我想将字符串数据发送到我已集成的服务引用。

ServiceReference3.Service1Client client = new ServiceReference3.Service1Client();
client.GetOrderAsync(strJson);

抛出以下异常:类型

的例外
  

' System.ServiceModel.CommunicationException'发生在   System.ServiceModel.ni.dll但未在用户代码中处理

异常地点:它打开reference.cs,并在此代码中抛出异常。

类型' System.ServiceModel.FaultException'的例外情况发生在System.ServiceModel.ni.dll中但未在用户代码中处理

public bool EndGetOrder(System.IAsyncResult result) 
 {
    object[] _args = new object[0];
    bool _result=((bool)(base.EndInvoke("GetOrder", _args, result)));//At this line exception occurs
    return _result;

 }

1 个答案:

答案 0 :(得分:0)

试试这个

var c = new HttpClient
{
    BaseAddress = new Uri("Your URL")
};

c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var json = "Your json string"

var req = new HttpRequestMessage(HttpMethod.Post, "Your Method name in URI Template")
{
    Content = new StringContent(json, Encoding.UTF8, "application/json")
};
c.SendAsync(req).ContinueWith(respTask =>
{
    var response = respTask.Result.Content.ReadAsStringAsync();
    Console.WriteLine("Response: {0}", respTask.Result);
});
}