http post方法调用接口获取json文件内容 以及获取Json字符串某节点的值
2017-03-15 14:23
871 查看
调用示例:
protected void btnPost_Click(object sender, EventArgs e)
{
string data = "{ \"touser\":\"" + OPENID + "\", \"msgtype\":\"news\", \"news\": {\"articles\": [{ \"title\":\"" + title + "\",\"description\":\"" + description + "\",\"url\":\"" + TurnUrl +
"\",\"picurl\":\"" + PicUrl + "\" ]}}";
string json = Post(URL, data);
//获取返回结果信息
string result =
GetJsonValue((json, "result", 0));
//下一步操作
}
//post方法调用接口获取json文件内容
public string Post(string URL, string Data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.Timeout = 30000;
request.ContentType = "application/json";
byte[] data = System.Text.Encoding.UTF8.GetBytes(Data);
Stream reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
HttpWebResponse res;
try
{
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string result = sr.ReadToEnd().Trim();
sr.Close();
return result;
}
/// <summary>
/// 获取Json字符串某节点的值
/// </summary>
public string GetJsonValue(string jsonStr, string key, int isdata)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗号,若是最后一个,截“}”号,取最小值
int end = jsonStr.IndexOf(',', index);
if (isdata == 1)
end = jsonStr.IndexOf(']', index);
else if (isdata == 2)
end = jsonStr.LastIndexOf(']', jsonStr.Length - 1);
else if (isdata == 3)
end = jsonStr.IndexOf('}', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
}
result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
}
}
return result;
}
protected void btnPost_Click(object sender, EventArgs e)
{
string data = "{ \"touser\":\"" + OPENID + "\", \"msgtype\":\"news\", \"news\": {\"articles\": [{ \"title\":\"" + title + "\",\"description\":\"" + description + "\",\"url\":\"" + TurnUrl +
"\",\"picurl\":\"" + PicUrl + "\" ]}}";
string json = Post(URL, data);
//获取返回结果信息
string result =
GetJsonValue((json, "result", 0));
//下一步操作
}
//post方法调用接口获取json文件内容
public string Post(string URL, string Data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.Timeout = 30000;
request.ContentType = "application/json";
byte[] data = System.Text.Encoding.UTF8.GetBytes(Data);
Stream reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
HttpWebResponse res;
try
{
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string result = sr.ReadToEnd().Trim();
sr.Close();
return result;
}
/// <summary>
/// 获取Json字符串某节点的值
/// </summary>
public string GetJsonValue(string jsonStr, string key, int isdata)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗号,若是最后一个,截“}”号,取最小值
int end = jsonStr.IndexOf(',', index);
if (isdata == 1)
end = jsonStr.IndexOf(']', index);
else if (isdata == 2)
end = jsonStr.LastIndexOf(']', jsonStr.Length - 1);
else if (isdata == 3)
end = jsonStr.IndexOf('}', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
}
result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
}
}
return result;
}
相关文章推荐
- httpurlconectiondopost的方法向服务器中获取json文件
- 如何调用http接口获取json数据及GET/POST方式调用http接口
- 通过HttpClient调用WebApi的Get、Post方法(返回类型限定为json格式)
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
- JAVA发送http get/post请求,调用http接口、方法
- Post/get 提交调用抓取Url获取json字符串
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- JAVA发送http get/post请求,调用http接口、方法
- PHP调用接口用post方法传送json数据
- 使用jQuery ajax post方法从Servlet获取json post方法中callback function不调用
- JS调用后台方法获取txt文件内容
- CURL实现HTTP的GET POST方法,以及上传文件
- php发送get、post请求的几种方法 以及获取内容
- C#使用Http Post方式传递Json数据字符串调用Web Service
- Android学习笔记---28_网络通信之通过HTTP协议实现文件上传,组拼http 的post方法,传输内容
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
- 如何开发一接口 要求是外界会通过POST方法向HTTP发送一个xml文件
- Windows下获取Dump文件以及进程下各线程调用栈的方法总结(转)
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求、调用http接口、方法