使用POST方法调用webservice(asmx)

时间:2018-06-28 12:35:15

标签: c# web-services http

嗨,我想从C#中的桌面应用程序调用Web应用程序的Web服务,但是我尝试了此操作,但它给内部服务器错误500提供了错误。Web服务的URL太长,因此我需要通过POST方法传递它。 我的代码有什么不对?请提出任何建议。

 private void button1_Click(object sender, EventArgs e)
        {
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL OF Webservice");
            //WebResponse response = request.GetResponse();
            //response.Close();

            using (var wb = new WebClient())
            {

                var data = new NameValueCollection();
                data["authU"] = "value";
                data["authP"] = "value";
                data["cmp"] = "value";
                data["sales_id"] = "value";
                data["Login"] = "value";
                data["total_amount"] = "value";
                data["total_discount"] = "value";
                data["net_amount"] = "value";
                data["change"] = "value";
                data["tax"] = "value";
                data["ip"] = "value";
                data["Tran_Type"] = "value";
                data["mac_id"] = "value";
                data["ref_id"] = "value";
                data["venue_id"] = "value";
                data["store_name"] = "value";
                data["actual_total_amount"] = "value";
                data["temp_sale_id"] = "value";
                data["is_return"] = "value";
                data["created_date"] = "2018-06-14 14:26:09";
                data["modify_date"] = "2018-06-14 14:26:09";
                data["mode"] = "value";
                data["value"] = "value";
                data["machine_id"] = "value";
                data["location_id"] = "value";
                data["input_amount"] = "value";
                data["sale_type"] = "value";
                data["is_table"] = "value";
                data["Payment_Date"] = "2018-06-14 14:26:09";
                data["Payment_Amount"] = "value";
                data["Table_name"] = "value";
                data["is_close"] = "value";
                data["values"] = "value";

                var response = wb.UploadValues("http://localhost:53653/POS_WebService.asmx/Sales_Master_Full", "POST", data);
                string responseInString = Encoding.UTF8.GetString(response);

2 个答案:

答案 0 :(得分:0)

将数据发布到Web服务不是与之交互的标准方法。在这种情况下,您指向WSDL的副本:

http://localhost:53653/POS_WebService.asmx?WSDL

,然后让VS创建一个用于调用函数的代理包装器。

有关如何添加参考的信息,请参见https://stackoverflow.com/a/12710348/656243。从那里开始,假设您将引用添加为WebService1,则只需将该函数作为Webservice的成员进行调用即可:

WebService1 svc = new Webservice1;
var data = svc.Sales_Master_Full();

就像正常的C#函数一样。

答案 1 :(得分:0)

您需要如下设置HTTP Content type标头:

using (var wb = new WebClient())
{
    wb.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

    var data = new NameValueCollection();
    // ...
    var response = wb.UploadValues("http://localhost:53653/POS_WebService.asmx/Sales_Master_Full", "POST", data);
    // ...
}