怎么从网站上获取json数据 c#代码

x05201314 2012-10-08 02:29:52
做 一个开奖器需要从网站上获取 json 数据 本人小白一个
刚接触
求解
...全文
474 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuboxue168 2015-01-27
  • 打赏
  • 举报
回复
怎么调用这个方法呀?
xiaole441657198 2013-09-25
  • 打赏
  • 举报
回复
json 数据必须反序列化回来
cyl2711535 2013-09-25
  • 打赏
  • 举报
回复
非常感谢haukwong 提供的代码分享的,今天学习了
Hauk 2012-10-08
  • 打赏
  • 举报
回复

private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
public static string referer = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html";

/// <summary>
/// 获取HTML
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="method">GET or POST</param>
/// <param name="postData">like "username=admin&password=123"</param>
/// <returns></returns>
public static string GetHtml(string url, CookieContainer cookieContainer, string method = "GET", string postData = "")
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;

if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
}

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();

streamReader.Close();
responseStream.Close();

httpWebRequest.Abort();
httpWebResponse.Close();

return html;
}
catch (Exception)
{
return string.Empty;
}
}


发送请求到目标地址就可以了。
Hauk 2012-10-08
  • 打赏
  • 举报
回复

private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
public static string referer = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html";

/// <summary>
/// 获取HTML
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="method">GET or POST</param>
/// <param name="postData">like "username=admin&password=123"</param>
/// <returns></returns>
public static string GetHtml(string url, CookieContainer cookieContainer, string method = "GET", string postData = "")
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;

if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
}

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();

streamReader.Close();
responseStream.Close();

httpWebRequest.Abort();
httpWebResponse.Close();

return html;
}
catch (Exception)
{
return string.Empty;
}
}


发送请求到目标地址就可以了。
bdmh 2012-10-08
  • 打赏
  • 举报
回复
google “c# 模拟提交”
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 Visual Studio (VS) 中,C# 项目读取 JSON 配置文件是一项常见且重要的任务。本文将详细讲解如何在 VS 中通过 C# 项目读取 JSON 配置文件。 在软件开发过程中,配置文件是不可或缺的一部分,它存储了应用程序的各种设置和参数。JSON 配置文件因其简洁性和易读性而被广泛应用于存储应用程序的配置信息。在 C# 项目中,读取 JSON 配置文件是确保应用程序正常运行的关键步骤。 在 C# 项目中,ConfigurationBuilder 是读取 JSON 配置文件的常用方法之一。它是 .NET Core 提供的一个组件,专门用于读取配置文件。以下是一个简单的示例代码: 在上述代码中,我们首先创建了一个 ConfigurationBuilder 对象,并通过 SetBasePath 方法设置了配置文件的基本路径。接着,使用 AddJsonFile 方法添加了 JSON 配置文件。最后,通过 Build 方法构建了一个 IConfiguration 对象,从而能够读取配置文件中的值。 在示例代码中,我们通过 IConfiguration 对象读取了配置文件中的数据。具体来说,可以使用 indexer 语法来获取配置文件中的值,例如: 上述代码展示了如何读取配置文件中的连接字符串、名称和 URL 等信息。 在实际开发中,读取 JSON 配置文件的方法可以应用于多种场景,例如读取数据库连接字符串、应用程序的设置以及日志配置等。通过这种方式,可以灵活地管理应用程序的配置信息,而无需修改代码。 本文介绍了在 VS 中使用 C# 项目读取 JSON 配置文件的方法。掌握读取 JSON 配置文件的技术对于开发高质量的应用程序至关重要。希望本文的内容能对您有所帮助。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧