1

hi i want to call my web application's web service from desktop application in c# i have tried this but it gives error internal server error 500.my URL of web service is too long so i need to pass it with POST method. what is not right in my code ? any suggestion please.

 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 Answers2

0

Posting data to web services isn't the standard way to interact with them. You point to a copy of the WSDL, in this case, at:

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

and let VS create a proxy wrapper for calling the functions.

See https://stackoverflow.com/a/12710348/656243 for how to add the reference. From there, assuming you added the reference as WebService1, you can just call the function as member of the webservice:

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

as if it were a normal C# function.

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • so i need to give .asmx file reference in this reference or particular webservice (sales_master_full) reference? – Nimisha Prajapati Jun 28 '18 at 12:48
  • Did you follow that link in the middle of my answer with explicit steps on how to add the web service? In that process, you'll be prompted for the URL, which in your case is: http://localhost:53653/POS_WebService.asmx?WSDL – Lynn Crumbling Jun 28 '18 at 12:50
  • i have added reference still get same error 'The remote server returned an error: (500) Internal Server Error.' – Nimisha Prajapati Jun 28 '18 at 13:00
  • no get error only The remote server returned an error: (500) Internal Server Error.The remote server returned an error: (500) Internal Server Error. this error – Nimisha Prajapati Jun 28 '18 at 13:19
  • I would contact the developer of the webservice, to try to understand why their code is erroring out. It's either because you are passing arguments into the function that the service doesn't understand, or because their code has a bug. – Lynn Crumbling Jun 28 '18 at 13:21
  • is this necessary to add this reference http://localhost:53653/POS_WebService.asmx?WSDL 'WSDL' after service string. sorry for silly question but i don't have knowledge of it – Nimisha Prajapati Jun 28 '18 at 13:22
  • Yes; that `?WSDL` is necessary to request the WSDL for the service. – Lynn Crumbling Jun 28 '18 at 13:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174002/discussion-between-nimisha-prajapati-and-lynn-crumbling). – Nimisha Prajapati Jun 29 '18 at 05:32
  • is it create any problem if web service in vb.net and my desktop application in C#? – Nimisha Prajapati Jun 29 '18 at 07:23
0

You need to set the HTTP Content type header as follows:

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);
    // ...
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35