1
votes

This is my first c# attempt in a while, but I have gotten myself to this point and maybe I think I need some expert eyes on it.

I keep getting the error mentioned in the title. I am guessing there is something obvious I am missing?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace API_Call
{
    class Program
    {
        static void Main(string[] args)
        {
            var watch = new Stopwatch();
            HttpWebRequest request = HttpWebRequest.Create("http://bk-pim/Perfion/GetData.asmx") as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/xml";
            request.Headers.Add("SOAPAction","http://perfion.com/ExecuteQuery");
            //FileInfo info = new FileInfo(@"C:\Users\dcoats\Desktop\mytestfile.txt");
            //long size = info.Length;
            //request.ContentLength = size;


            using (var stream = request.GetRequestStream())
            using (var writer = new StreamWriter(stream))
            {
                var body = WebUtility.HtmlEncode(File.ReadAllText(@"C:\Users\dcoats\Desktop\mytestfile.txt"));
                body = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>< soap:Envelope xmlns:soap = \"http://schemas.xmlsoap.org/soap/envelope/\" xmlns: xsi = \"http://www.w3.org/2001/XMLSchema-instance\" xmlns: xsd = \"http://www.w3.org/2001/XMLSchema\" >< soap:Body >< ExecuteQueryResponse xmlns = \"http://perfion.com/\" >< ExecuteQueryResult >" + body + "</ExecuteQueryResult></ ExecuteQueryResponse ></ soap:Body ></ soap:Envelope > ";

            }


            watch.Start();
            var response = request.GetResponse(); //bombs here
            watch.Stop();
            response.GetResponseStream();

            Console.WriteLine();
            Console.ReadLine();
            Console.WriteLine(watch.ElapsedMilliseconds);
            Console.ReadLine();
        }
    }
}
1
Why did you create a StreamWriter? You don't seem to use it anywhere. I think it's time to clean up your code. - Jonathan Wood
You are not writing body to writer at all. - Ilian
What are you actually trying to do though? There are better easiest ways to interact with a web service from c# than pushing string-concat-formed xml down a socket by hand - Caius Jard
@CaiusJard well thats exactly what Im trying to do. Seeing in how Im not that proficient at c# I kind of found myself googling my way into it. I mean, if you have better methods Im all ears. - Doug Coats

1 Answers

1
votes

You asked for a better way to interact with a webservice:

  • Right click REFERENCES in SOLUTION EXPLORER
  • Choose ADD SERVICE REFERENCE
  • Enter the URL where your service is located - example http://bk-pim/Perfion/GetData.asmx or http://www.dneonline.com/calculator.asmx
  • Choose a namespace/set other options
  • Click OK
  • Create a client (I couldn't use your webservice, so this answer now switches to using DNEOnline's calculator, with a chosen namespace of "Calc" ):

Calc.CalculatorSoapClient sc = new Calc.CalculatorSoapClient();

  • Call methods on the client:

int result = sc.Add(1,2);

That's it - no sockets, no building XML etc - the service reference creates a set of objects and classes that do all of that