C# 在将所有字节写入c之前无法关闭流#
这是我一段时间以来的第一次c#尝试,但我已经做到了这一点,也许我认为我需要一些专家的眼光来研究它 我一直得到标题中提到的错误。我猜有什么明显的东西我遗漏了C# 在将所有字节写入c之前无法关闭流#,c#,httpwebrequest,streamwriter,C#,Httpwebrequest,Streamwriter,这是我一段时间以来的第一次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.Ta
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web;
名称空间API_调用
{
班级计划
{
静态void Main(字符串[]参数)
{
var watch=新秒表();
HttpWebRequest请求=HttpWebRequest.Create(“http://bk-pim/Perfion/GetData.asmx)作为HttpWebRequest;
request.Method=“POST”;
request.ContentType=“text/xml”;
添加(“SOAPAction”http://perfion.com/ExecuteQuery");
//FileInfo=newfileinfo(@“C:\Users\dcoats\Desktop\mytestfile.txt”);
//长尺寸=信息长度;
//request.ContentLength=大小;
使用(var stream=request.GetRequestStream())
使用(var writer=新的StreamWriter(流))
{
var body=WebUtility.HtmlEncode(File.ReadAllText(@“C:\Users\dcoats\Desktop\mytestfile.txt”);
body=“”+body+”;
}
watch.Start();
var response=request.GetResponse();//此处
看,停;
response.GetResponseStream();
Console.WriteLine();
Console.ReadLine();
控制台写入线(手表延时毫秒);
Console.ReadLine();
}
}
}
您要求使用更好的方式与Web服务交互:
- 在解决方案资源管理器中的引用上单击鼠标右键
- 选择添加服务引用
- 输入服务所在的URL-示例或
- 选择名称空间/设置其他选项
- 单击“确定”
- 创建一个客户机(我无法使用您的Web服务,所以这个答案现在切换到使用DNEOnline的计算器,选择名称空间为“Calc”):
Calc.CalculatorSoapClient sc=新的Calc.CalculatorSoapClient()代码>
- 客户端上的调用方法:
int result=sc.Add(1,2)代码>
就是这样-没有套接字,没有构建XML等-服务引用创建了一组对象和类来完成所有这些为什么要创建一个StreamWriter
?你似乎没有在任何地方使用它。我想是时候清理一下你的代码了。你根本没有把body
写给writer
。你到底想做什么?从c#与web服务交互的最简单的方法比通过将字符串concat格式的xml推送到套接字中更好hand@CaiusJard这正是我想做的。看到我对c#不是那么精通,我发现自己用谷歌搜索了一下。我的意思是,如果你有更好的方法,我会洗耳恭听。我会试试这个,看看效果如何。我能够解决我的问题,但我确实喜欢简单;将一组对象序列化和反序列化到XML和从XML序列化和反序列化要比通过分割字符串将其组合在一起正确得多:)