WinForm里怎么使用Http把数据发送到WebService里 各位大虾: 现在要实现的如下功能: WinForm里怎么使用Http把数据发送到WebService里,然后在webservice里处理数据并把数据保存到数据库里,望各位大虾给点建议,或是给段源码 谢谢了!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 service端namespace TestService.Service{ /// <summary> /// Server 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class Server : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } }}请求方protected void Button1_Click(object sender, EventArgs e) { string s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ " <soap:Body>"+ " <HelloWorld xmlns=\"http://tempuri.org/\" />"+ " </soap:Body>"+ "</soap:Envelope>"; string url = "http://localhost/TestService/Service/Server.asmx"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ContentType = "text/xml; charset=utf-8"; request.Method = "post"; request.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld"); byte[] content = System.Text.Encoding.UTF8.GetBytes(s); request.ContentLength = content.Length; Stream rs = request.GetRequestStream(); rs.Write(content, 0, content.Length); rs.Close(); WebResponse response = request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); this.result.Text = sr.ReadToEnd(); }这是最简单的方式,如果有自定义的数据结构,还要操作序列化和反序列的部分。 其实最简单的方法是添加Web reference... 或者 Service Reference...然后直接使用vs生成的代理类。 可以用WCF,建一个WCF服务端,放在IIS中在客户端可以调用服务端的方法和服务端通信了。 谢楼上的回答我在winfrom里的数据都是在List<T>组里的,这里有多个数组1楼大哥的方法 我觉得对我目前最有用,可该怎么把这些数据在请求方获得并发送过去,并且在接收方接收并处理弄到数据库(目前不用考虑)呢?1楼的大哥 能给点相关的资料不? WebService这么用就太浪费了,LZ直接像2楼的那样不行吗?什么理由非要绕弯路呢? 如果是直接用HttpRequest的话,我建议lz看一下RESTful的WebService设计。或者直接设计几个Asp.net页面,请求数据用URL或者FormData发过去 谢fangxinggood的回答因为我对webservice这个用法一点都不清楚,还是昨天看了些资料,才知道一些概念所以对于2楼大哥的回答我是不怎么明白,直接饮用webservice,如果在详细点就好了用webservice是公司老总的要求,这也是没办法的事,6楼的大哥能给点资料的链接不?谢谢!! 我还以为你有什么特殊的需求呢?要用httprequest的方式发送soap请求,原来你是不知道怎么用啊。确实用httprequest发送请求太浪费。你直接在你的项目上右键 -> 添加web引用 -> 在url地址处添加服务的地址 -> 点击“前往”,如果地址正确就会看到服务了,然后为他们添加一个本地的命名空间,系统会自动生成本地代理类。你调用远端服务方法时,就 命名空间.类名 就ok了 如果用WebService还要自己拼Xml的话,就没有意义了 关于picture box 的使用问题求解 我下载了一个网站国际化的DEMO,但不知道怎么用 高分求IBatisNet使用一段时间出现的问题 求.NET的一个排列组合 问个Quartz.net的问题,怎么判断一个任务已经执行完了? 用Socket发送数据如何处理中文字符?急…… 请问在用SQLConnection连接到SQLSERVER时,如何在程序中设置端口 Func<T1,T2,R1> 如何对带有out参数的方法使用。 CMD运行程序路径双引号的问题 如何实现xp界面 在C#中HttpClient的命名空间是什么 iis7.0 设置不检查文件是否存在
{
/// <summary>
/// Server 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Server : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
请求方protected void Button1_Click(object sender, EventArgs e)
{
string s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ " <soap:Body>"
+ " <HelloWorld xmlns=\"http://tempuri.org/\" />"
+ " </soap:Body>"
+ "</soap:Envelope>"; string url = "http://localhost/TestService/Service/Server.asmx"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ContentType = "text/xml; charset=utf-8";
request.Method = "post";
request.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld"); byte[] content = System.Text.Encoding.UTF8.GetBytes(s); request.ContentLength = content.Length; Stream rs = request.GetRequestStream(); rs.Write(content, 0, content.Length); rs.Close(); WebResponse response = request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); this.result.Text = sr.ReadToEnd();
}
这是最简单的方式,如果有自定义的数据结构,还要操作序列化和反序列的部分。
建一个WCF服务端,放在IIS中
在客户端可以调用服务端的方法和服务端通信了。
我在winfrom里的数据都是在List<T>组里的,这里有多个数组
1楼大哥的方法 我觉得对我目前最有用,可该怎么把这些数据在请求方获得并发送过去,并且在接收方接收并处理弄到数据库(目前不用考虑)呢?
1楼的大哥 能给点相关的资料不?
或者直接设计几个Asp.net页面,请求数据用URL或者FormData发过去
因为我对webservice这个用法一点都不清楚,还是昨天看了些资料,才知道一些概念
所以对于2楼大哥的回答我是不怎么明白,直接饮用webservice,如果在详细点就好了用webservice是公司老总的要求,这也是没办法的事,6楼的大哥能给点资料的链接不?谢谢!!
我还以为你有什么特殊的需求呢?要用httprequest的方式发送soap请求,原来你是不知道怎么用啊。
确实用httprequest发送请求太浪费。
你直接在你的项目上右键 -> 添加web引用 -> 在url地址处添加服务的地址 -> 点击“前往”,如果地址正确就会看到服务了,然后为他们添加一个本地的命名空间,系统会自动生成本地代理类。你调用远端服务方法时,就 命名空间.类名 就ok了