如何在.net中模拟post方式提交json数据
2个回答
展开全部
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Cache;
using System.Text;
public class Spider
{
public static string GetHtmlByJson(string url, string json = "")
{
var result = string.Empty;
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "text/json";
request.Method = "post";
//request.CookieContainer = _cookie;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
}
catch (UriFormatException uex)
{
// 出错处理
}
catch (WebException ex)
{
// 出错处理
}
return result;
}
}
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Cache;
using System.Text;
public class Spider
{
public static string GetHtmlByJson(string url, string json = "")
{
var result = string.Empty;
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "text/json";
request.Method = "post";
//request.CookieContainer = _cookie;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
}
catch (UriFormatException uex)
{
// 出错处理
}
catch (WebException ex)
{
// 出错处理
}
return result;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询