c#+xmlhttp调用webservice

本文介绍如何在Visual Studio 2005中通过GET和POST方法调用WebService服务。具体包括使用MSXML2组件进行HTTP请求的方法,并详细展示了如何构造SOAP消息以及设置必要的请求头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. webservice还是用http://netboy.cnblogs.com/archive/2006/02/18/333260.html。^_^,这就叫复    用!自己先吐下!

2.偶用的是vs2005,首先当然是添加MSXML2的com组件了,地球人都知道。下面代码直接上了,^_^。

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif using  FormChild;
 9 None.gif using  MSXML2;
10 None.gif
11 None.gif namespace  FormParent
12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
13InBlock.gif    public partial class Form1 : Form
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif        public Form1()
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            InitializeComponent();
18ExpandedSubBlockEnd.gif        }

19InBlock.gif
20InBlock.gif        private void Form1_Load(object sender, EventArgs e)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            
23ExpandedSubBlockEnd.gif        }

24InBlock.gif
25InBlock.gif        private void btnGet_Click(object sender, EventArgs e)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
28InBlock.gif            xmlhttp.open("GET""http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach"falsenullnull);
29InBlock.gif            xmlhttp.send("");
30InBlock.gif            MSXML2.XMLDocument dom = new XMLDocument();
31InBlock.gif            Byte[] b = (Byte[])xmlhttp.responseBody;
32InBlock.gif           
33InBlock.gif            string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);
34InBlock.gif            MessageBox.Show(s);
35ExpandedSubBlockEnd.gif        }

36InBlock.gif
37InBlock.gif        private void btnPost_Click(object sender, EventArgs e)
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            string strData = @"<?xml version='1.0' encoding='utf-8'?>
40InBlock.gif                                <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/'>
41InBlock.gif                                  <soap:Body>
42InBlock.gif                                    <SayHelloTo xmlns='http://tempuri.org/'>
43InBlock.gif                                      <Name>Zach</Name>
44InBlock.gif                                    </SayHelloTo>
45InBlock.gif                                  </soap:Body>
46InBlock.gif                                </soap:Envelope>";
47InBlock.gif            strData = strData.Replace("'""\"");
48InBlock.gif                       
49InBlock.gif            MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
50InBlock.gif            xmlhttp.open("POST""http://localhost:1323/WebSite6/Service.asmx"falsenullnull);
51InBlock.gif            xmlhttp.setRequestHeader("Content-Type""text/xml; charset=utf-8");
52InBlock.gif            xmlhttp.setRequestHeader("SOAPAction""http://tempuri.org/SayHelloTo");//Please add it,or you will get 500 error.
53InBlock.gif            xmlhttp.send(strData);
54InBlock.gif           
55InBlock.gif            Byte[] b = (Byte[])xmlhttp.responseBody;
56InBlock.gif            string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);;
57InBlock.gif            MessageBox.Show(s);
58InBlock.gif
59ExpandedSubBlockEnd.gif        }

60InBlock.gif
61ExpandedSubBlockEnd.gif      }

62ExpandedBlockEnd.gif}

使用post方法访问的时候必须加:
 xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/SayHelloTo");
有点疑惑,用get方法为什么就可以不加?这算什么?

3.题外话:
最开始我测试Get方式是放在Form_Load里,post方法是放在button_Click,后来我就在窗体上按住Crlf拖动复制了原来的按钮,然后双击打开……奇怪,咱们双击两个按钮总是定位到原来的button_click的处理过程。bug? 2003好像不是这样的。

转载于:https://www.cnblogs.com/netboy/archive/2006/02/20/334323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值