c#+xmlhttp调用webservice
1. webservice还是用http://netboy.cnblogs.com/archive/2006/02/18/333260.html。^_^,这就叫复 用!自己先吐下!
2.偶用的是vs2005,首先当然是添加MSXML2的com组件了,地球人都知道。下面代码直接上了,^_^。
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using FormChild;
9using MSXML2;
10
11namespace FormParent
12{
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void Form1_Load(object sender, EventArgs e)
21 {
22
23 }
24
25 private void btnGet_Click(object sender, EventArgs e)
26 {
27 MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
28 xmlhttp.open("GET", "http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach", false, null, null);
29 xmlhttp.send("");
30 MSXML2.XMLDocument dom = new XMLDocument();
31 Byte[] b = (Byte[])xmlhttp.responseBody;
32
33 string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);
34 MessageBox.Show(s);
35 }
36
37 private void btnPost_Click(object sender, EventArgs e)
38 {
39 string strData = @"<?xml version='1.0' encoding='utf-8'?>
40 <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/'>
41 <soap:Body>
42 <SayHelloTo xmlns='http://tempuri.org/'>
43 <Name>Zach</Name>
44 </SayHelloTo>
45 </soap:Body>
46 </soap:Envelope>";
47 strData = strData.Replace("'", "\"");
48
49 MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
50 xmlhttp.open("POST", "http://localhost:1323/WebSite6/Service.asmx", false, null, null);
51 xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
52 xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/SayHelloTo");//Please add it,or you will get 500 error.
53 xmlhttp.send(strData);
54
55 Byte[] b = (Byte[])xmlhttp.responseBody;
56 string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);;
57 MessageBox.Show(s);
58
59 }
60
61 }
62}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using FormChild;
9using MSXML2;
10
11namespace FormParent
12{
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void Form1_Load(object sender, EventArgs e)
21 {
22
23 }
24
25 private void btnGet_Click(object sender, EventArgs e)
26 {
27 MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
28 xmlhttp.open("GET", "http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach", false, null, null);
29 xmlhttp.send("");
30 MSXML2.XMLDocument dom = new XMLDocument();
31 Byte[] b = (Byte[])xmlhttp.responseBody;
32
33 string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);
34 MessageBox.Show(s);
35 }
36
37 private void btnPost_Click(object sender, EventArgs e)
38 {
39 string strData = @"<?xml version='1.0' encoding='utf-8'?>
40 <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/'>
41 <soap:Body>
42 <SayHelloTo xmlns='http://tempuri.org/'>
43 <Name>Zach</Name>
44 </SayHelloTo>
45 </soap:Body>
46 </soap:Envelope>";
47 strData = strData.Replace("'", "\"");
48
49 MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
50 xmlhttp.open("POST", "http://localhost:1323/WebSite6/Service.asmx", false, null, null);
51 xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
52 xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/SayHelloTo");//Please add it,or you will get 500 error.
53 xmlhttp.send(strData);
54
55 Byte[] b = (Byte[])xmlhttp.responseBody;
56 string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);;
57 MessageBox.Show(s);
58
59 }
60
61 }
62}
使用post方法访问的时候必须加:
xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/SayHelloTo");
有点疑惑,用get方法为什么就可以不加?这算什么?
3.题外话:
最开始我测试Get方式是放在Form_Load里,post方法是放在button_Click,后来我就在窗体上按住Crlf拖动复制了原来的按钮,然后双击打开……奇怪,咱们双击两个按钮总是定位到原来的button_click的处理过程。bug? 2003好像不是这样的。