string jsonstr="待发送json格式的字符串";

string host = "地址";

if (string.IsNullOrEmpty(host))
{
throw new Exception("服务器地址不得为空.");
}

string url = string.Format("http://{0}/", host);

string json = string.Empty;

try
{
//序列化
json = JsonConvert.SerializeObject(jsonstr);
}
catch (Exception)
{

}

//创建一个XMLHTTP对象

XMLHTTP xmlhttp = new XMLHTTP();
xmlhttp.open("POST", url, false, null, null);
xmlhttp.setRequestHeader("Content-Type", "text/json; charset=utf-8");
//xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/SayHelloTo");//Please add it,or you will get 500 error.
xmlhttp.send(json);

//返回某一格式的服务器响应数据
Byte[] b = (Byte[])xmlhttp.responseBody;
string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length); ;
Console.WriteLine(s);

posted on 2012-11-09 11:32  元霸  阅读(850)  评论(0)    收藏  举报