简体   繁体   English

不向Web服务发送数据

[英]Not sending data to Web Service

Here's my code for sending the data. 这是我发送数据的代码。 There is a string strJson i want to send the string data to the service reference i have integrated. 有一个字符串strJson我想将字符串数据发送到我已经集成的服务引用。

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

It throws Following Exception:An exception of type 它抛出以下异常:类型的异常

'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.ni.dll but was not handled in user code System.ServiceModel.ni.dll中发生'System.ServiceModel.CommunicationException'但未在用户代码中处理

Exception place:It opens reference.cs and in that at this code it throws following exception. 异常地点:它打开reference.cs,并在此代码中抛出异常。

An exception of type 'System.ServiceModel.FaultException' occurred in System.ServiceModel.ni.dll but was not handled in user code System.ServiceModel.ni.dll中出现“System.ServiceModel.FaultException”类型的异常,但未在用户代码中处理

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;

 }

Try this 尝试这个

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);
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM