这是我一段时间以来的第一次C#尝试,但是我已经明白了这一点,也许我认为我需要一些专家的关注。
我一直收到标题中提到的错误。我猜有明显的东西我想念吗?
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();
}
}
}
答案 0 :(得分:1)
您要求与Web服务进行交互的更好方法:
Calc.CalculatorSoapClient sc = new Calc.CalculatorSoapClient();
int result = sc.Add(1,2);
就是这样-没有套接字,没有构建XML等-服务引用创建了一组对象和类来完成所有这些工作