Web Service接口方法
[WebMethod]
public string Project(string paramaters)
{
return paramaters;
}
实现代码
public string Post(string methodName, string jsonParas)
{
string strURL = Url + "/" + methodName;
//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
//内容类型
request.ContentType = "application/x-www-form-urlencoded";
//设置参数,并进行URL编码
//虽然我们需要传递给服务器端的实际参数是JsonParas(格式:[{\"UserID\":\"0206001\",\"UserName\":\"ceshi\"}]),
//但是需要将该字符串参数构造成键值对的形式(注:"paramaters=[{\"UserID\":\"0206001\",\"UserName\":\"ceshi\"}]"),
//其中键paramaters为WebService接口函数的参数名,值为经过序列化的Json数据字符串
//最后将字符串参数进行Url编码
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
byte[] payload;
//将Json字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
request.ContentLength = payload.Length;
//发送请求,获得请求流
Stream writer;
try
{
writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
}
catch (Exception)
{
writer = null;
Console.Write("连接服务器失败!");
}
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
writer.Close();//关闭请求流
String strValue = "";//strValue为http响应所返回的字符流
HttpWebResponse response;
try
{
//获得响应流
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Stream s = response.GetResponseStream();
//服务器端返回的是一个XML格式的字符串,XML的Content才是我们所需要的Json数据
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
strValue = Reader.ReadInnerXml();//取出Content中的Json数据
Reader.Close();
s.Close();
return strValue;//返回Json数据
}
Url的格式样例:"http://59.68.29.106:8087/IFT_Project.asmx"
methodName参数就是"Project"
JsonParas就是使用C# JavaScriptSerializer将List
我之前做过网络和桌面应用的数据对接,你看看
/*
1、对象集合
*/
[DataContract]
public class Items
{
[DataMember]
public Listddd{get;set;}
public Items()
{
ddd = new List();
}
//把字符串转换为对象
public static Items FormJson(string json)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer a = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Items));
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
return (Items)a.ReadObject(stream);
}
}
catch (Exception)
{
return null;
}
}
}
/*
简单的对象
*/
[DataContract]
public class ddd
{
[DataMember]
public int cid{get;set;}
[DataMember]
public int pid { get; set; }
[DataMember]
public int oid { get; set; }
[DataMember]
public string view_type { get; set; }
[DataMember]
public string status { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string eng_name { get; set; }
[DataMember]
public string ctpl { get; set; }
[DataMember]
public string url { get; set; }
}
/*
2、从HTTP请求中得到数据
*/
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.bursonchurch.net//index.php?Interface-index-keyt-123-act-catelist.html");
request.Timeout = 5000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("内容类型:"+response.ContentType);
Console.WriteLine("内容长度:"+response.ContentLength);
Console.WriteLine("服务器名:"+response.Server);
Console.WriteLine("资源的URI:"+response.ResponseUri);
Console.WriteLine("HTTP协议版本:" + response.ProtocolVersion);
Console.WriteLine("相应状态:"+response.StatusCode);
Console.WriteLine("相应方法:"+response.Method);
//头信息
for (int i = 0; i < response.Headers.Count; ++i)
{
Console.WriteLine("\nHeader Name:{0},----------Value:{1}", response.Headers.Keys[i], response.Headers[i]);
}
StreamReader sr = new StreamReader(response.GetResponseStream());
string jsonstr = sr.ReadToEnd();
//例子 :假设得到的json数据 下面序列化为对象
string test = "{\"ddd\":[{\"cid\":\"1\",\"pid\":\"0\",\"oid\":\"0\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"西科推荐\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-1.html\"},{\"cid\":\"2\",\"pid\":\"0\",\"oid\":\"2\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"校园活动\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-2.html\"},{\"cid\":\"3\",\"pid\":\"0\",\"oid\":\"2\",\"view_type\":\"0\",\"status\":\"1\",\"name\":\"帅哥\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"动物世界keword\",\"cdescription\":\"动物世界s\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-3.html\"},{\"cid\":\"4\",\"pid\":\"0\",\"oid\":\"4\",\"view_type\":\"1\",\"status\":\"1\",\"name\":\"静物\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-4.html\"},{\"cid\":\"5\",\"pid\":\"0\",\"oid\":\"5\",\"view_type\":\"1\",\"status\":\"1\",\"name\":\"美女\",\"eng_name\":\"\",\"ctpl\":\"\",\"ctitle\":\"\",\"ckeywords\":\"\",\"cdescription\":\"\",\"url\":\"http:\\/\\/www.bursonchurch.net\\/index.php?Cate-index-cid-5.html\"}]}";
//第一种转
JavaScriptSerializer j = new JavaScriptSerializer();
Items ii = new Items();
ii = j.Deserialize(jsonstr);
//第二种也行
Items it = Items.FormJson(jsonstr);
response.Close();
Console.ReadKey();