使用HTTP-GET方式访问
使用HTTP-POST方式访问
使用SOAP方式访问
HTTP-GET方式访问#region HTTP-GET方式访问
private void button1_Click(Object sender,System.EventArgs e)
{
System.Net.HttpWebRequest Req;
string strGet="http://localhost/HelloWord?name=";
strGet=strGet+textBox1.Text;
Rep=(System.net.HttpWebRequest)WebRequest.Create(strGet);
System.Net.HttpWebResponse response;
response=(System.net.HttpWebResponse).Req.GetResponse();
System.IO.Stream s;
s=response.GetResponseStream();
XmlTextReader Reader=new XmlTextReader(s);
Reader.MoveToContent();
label1.Text=Reader.ReaderInnerXml();
Reader.Close();
}
#endregion
private void button1_Click(Object sender,System.EventArgs e)
{
System.Net.HttpWebRequest Req;
string strGet="http://localhost/HelloWord?name=";
strGet=strGet+textBox1.Text;
Rep=(System.net.HttpWebRequest)WebRequest.Create(strGet);
System.Net.HttpWebResponse response;
response=(System.net.HttpWebResponse).Req.GetResponse();
System.IO.Stream s;
s=response.GetResponseStream();
XmlTextReader Reader=new XmlTextReader(s);
Reader.MoveToContent();
label1.Text=Reader.ReaderInnerXml();
Reader.Close();
}
#endregion
使用HTTP-POST方式访问
HTTP-POST方式访问#region HTTP-POST方式访问
private void button1_Click(Object sender,System.EventArgs e)
{
string url;
url="http://localhost/HelloWord";
HttpWebRequest Req;
Req=(HttpWebRequest)WebRequest.Create(url);
//POST方式请求
Rep.Method="POST";
//内容类型
Rep.ContentType="application/x-www-form-urlencoded";
//参数经过URL编码
string paraUrlCoded=System.Web.HttpUtility.UrlEncode("name");
paraUrlCoded=paraUrlCoded+"="+System.Web.HttpUtility.UrlEncode(textBox1.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload=System.TextEncoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
Req.ContentLength=payload.Length;
//获得请求流
Stream writer=Req.GetRequestStream();
//将请求参数写入流
writer.Write(payload,0,payload.Length);
//关闭请求流
writer.Close();
//获得响应流
HttpWebResponse response=(HttpWebResponse)Req.GetResponse();
System.IO.Stream s;
s=response.GetResponseStream();
XmlTextReader Reader=new XmlTextReader(s);
Reader.MoveToContent();
label1.Text=Reader.ReaderInnerXml();
Reader.Close();
}
#endregion
private void button1_Click(Object sender,System.EventArgs e)
{
string url;
url="http://localhost/HelloWord";
HttpWebRequest Req;
Req=(HttpWebRequest)WebRequest.Create(url);
//POST方式请求
Rep.Method="POST";
//内容类型
Rep.ContentType="application/x-www-form-urlencoded";
//参数经过URL编码
string paraUrlCoded=System.Web.HttpUtility.UrlEncode("name");
paraUrlCoded=paraUrlCoded+"="+System.Web.HttpUtility.UrlEncode(textBox1.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload=System.TextEncoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
Req.ContentLength=payload.Length;
//获得请求流
Stream writer=Req.GetRequestStream();
//将请求参数写入流
writer.Write(payload,0,payload.Length);
//关闭请求流
writer.Close();
//获得响应流
HttpWebResponse response=(HttpWebResponse)Req.GetResponse();
System.IO.Stream s;
s=response.GetResponseStream();
XmlTextReader Reader=new XmlTextReader(s);
Reader.MoveToContent();
label1.Text=Reader.ReaderInnerXml();
Reader.Close();
}
#endregion
使用SOAP方式访问
首先客户代理用XmlSerializer对象进行参数序列化为XML.服务器端相应的
XmlSerializer对象反序列化,从而从XML中获取调用参数.
如下:Person类
Person类#region Person类
public class Person
{
private String name;
private int age;
public String Name;
{
get
{
return name;
}
set
{
name=value;
}
}
public int Age
{
get
{
return age;
}
set
{
age=value;
}
}
public void Show()
{
Console.WriteLine("Name"={0},Age={1},Name,Age);
}
}
#endregion
public class Person
{
private String name;
private int age;
public String Name;
{
get
{
return name;
}
set
{
name=value;
}
}
public int Age
{
get
{
return age;
}
set
{
age=value;
}
}
public void Show()
{
Console.WriteLine("Name"={0},Age={1},Name,Age);
}
}
#endregion
SOAP方式访问#region SOAP方式访问
static void Main(String[] args)
{
//实例化XML序列器
XmlSerializer serializer=net XmlSerializer(typeof(Person));
//生成序列化对象
Person p=new Person();
p.Age=22;
p.Name="Jack.Xiao";
//生成内存流
MemoryStream s=new MemoryStream();
//书写器
StreamWriter writer=new StreamWriter(s,Encoding.UTF8);
serializer.Serialize(writer,p);
//定位到流的头
s.Seek(0,SeekOrigin.Begin);
StreamReader reader=new StreamReader(s,Encoding.UTF8);
Person p2=(Person)serializer.Deserialize(reader);
p2.Show();
Console.WriteLine("==========XML内容如下===============");
//显示XML内容
s.Seek(0,SeekOrigin.Begin);
String xmls=reader.ReadLine()
while(xmls!=null)
{
Console.WriteLine(xmls);
xmls=reader.ReadLine();
}
//关闭流
reader.Close();
}
#endregion
static void Main(String[] args)
{
//实例化XML序列器
XmlSerializer serializer=net XmlSerializer(typeof(Person));
//生成序列化对象
Person p=new Person();
p.Age=22;
p.Name="Jack.Xiao";
//生成内存流
MemoryStream s=new MemoryStream();
//书写器
StreamWriter writer=new StreamWriter(s,Encoding.UTF8);
serializer.Serialize(writer,p);
//定位到流的头
s.Seek(0,SeekOrigin.Begin);
StreamReader reader=new StreamReader(s,Encoding.UTF8);
Person p2=(Person)serializer.Deserialize(reader);
p2.Show();
Console.WriteLine("==========XML内容如下===============");
//显示XML内容
s.Seek(0,SeekOrigin.Begin);
String xmls=reader.ReadLine()
while(xmls!=null)
{
Console.WriteLine(xmls);
xmls=reader.ReadLine();
}
//关闭流
reader.Close();
}
#endregion