1.http://www.tuling123.com/openapi/api?key=9d2ff29d44b54e55acadbf5643569584&info=?
上面这个请求在服务端发起
/// <summary>
/// 构造url的参数ajax的data值
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
public static string GetUrlStr(Dictionary<string, string> dic) {
StringBuilder sb = new StringBuilder();
foreach (var item in dic) {
sb.AppendFormat("{0}={1}&", item.Key, item.Value);
}
string urlStr = sb.Remove(sb.Length - 1, 1).ToString();
return urlStr;
}
/// <summary>
/// 发送post请求
/// </summary>
/// <param name="postData"></param>
/// <param name="postUrl"></param>
/// <returns></returns>
public static string SendHttpPostRequest(string postData,string postUrl) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
byte[] data = Encoding.GetEncoding("gbk").GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream()) {
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
return responseString;
}
2.将参数进行编码后调用
protected void Page_Load(object sender, EventArgs e) {
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("info", System.Web.HttpUtility.UrlEncode("笑话"));
string paream = Utility.GetUrlStr(test);
string result = Utility.SendHttpPostRequest(paream, "http://www.tuling123.com/openapi/api?key=9d2ff29d44b54e55acadbf5643569584");
Response.Write(result);
}
3.结果