不向Web服务发送数据 [英] Not sending data to Web Service
问题描述
下面是我的发送数据的代码。有一个字符串strJson我想字符串数据发送到服务引用我已经集成。
ServiceReference3.Service1Client客户端=新ServiceReference3.Service1Client();
client.GetOrderAsync(strJson);
它抛出以下异常:类型
的例外
System.ServiceModel.CommunicationException'发生在
System.ServiceModel.ni.dll但在用户代码中没有处理
块引用>
异常的地方:它打开reference.cs并且在这段代码就抛出异常以下
类型的异常系统.ServiceModel.FaultException发生在System.ServiceModel.ni.dll但在用户代码
公共BOOL EndGetOrder(系统没有处理.IAsyncResult结果)
{
对象[] = _args新的对象[0];
布尔_result =((布尔)(base.EndInvoke(GetOrder,_args,结果))); //在这一行发生异常
返回_result;
}
解决方案试试这个
变种C =新的HttpClient
{
BaseAddress =新的URI(你的URL)
};
c.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON));
VAR JSON =你的JSON字符串
变种REQ =新HttpRequestMessage(HttpMethod.Post,在URI模板方法名称)
{
含量=新的StringContent(JSON,Encoding.UTF8,应用/ JSON)
};
c.SendAsync(REQ).ContinueWith(respTask =>
{
VAR响应= respTask.Result.Content.ReadAsStringAsync();
Console.WriteLine(回应: {0},respTask.Result);
});
}
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.
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
Exception place:It opens reference.cs and in that at this code it throws following exception.
An exception of type 'System.ServiceModel.FaultException' occurred in System.ServiceModel.ni.dll but was not handled in user code
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); }); }
这篇关于不向Web服务发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!