using System;
using System.Collections.Generic;namespace SnatchScore
{
public partial class Tournament : System.Web.UI.Page
{
protected string _PostUrl = "https://www.digibet.com/preview/content/sportsbet/ajax/sportsbet.php";
protected void Page_Load(object sender, EventArgs e)
{
InitForm();
} void InitForm()
{
var param = new Dictionary<string, string>();
param.Add("type", "0");
param.Add("program", "20100038.TODAY");
param.Add("mode", "3");
var postReturnValue = GetPostBackStream(_PostUrl, param);
Response.Write(postReturnValue);
} /// <summary>
/// 取Form Port数据
/// </summary>
/// <param name="rUrl"></param>
/// <returns></returns>
public static string GetPostBackStream(string url, Dictionary<string, string> param)
{
var client = new System.Net.WebClient();
var nameValueCollection = new System.Collections.Specialized.NameValueCollection(); foreach (var item in param)
{
nameValueCollection.Add(item.Key, item.Value);
}
byte[] responseArray = client.UploadValues(url, "POST", nameValueCollection);
return System.Text.Encoding.ASCII.GetString(responseArray);
}
}
}代码如上
提示:
远程服务器返回错误: (403) 已禁止。那要怎么改才能正确的取到网页
https://www.digibet.com/preview/content/sportsbet/ajax/sportsbet.php
的数据我用HTML方式写POST是可以得到数据
但用C#代码写就报上面的错误谢谢
急
添加http头或cookie试试
{
var req = System.Net.WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string paramValue = string.Empty;
foreach (var item in param)
{
if (!string.IsNullOrEmpty(paramValue))
{
paramValue += "&";
}
paramValue = paramValue + item.Key + "=" + System.Web.HttpContext.Current.Server.UrlEncode(item.Value);
}
req.ContentLength = paramValue.Length; System.IO.StreamWriter stOut = new System.IO.StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(paramValue);
stOut.Close(); System.IO.StreamReader stIn = new System.IO.StreamReader(req.GetResponse().GetResponseStream());
string result = stIn.ReadToEnd();
stIn.Close();
return result;
}
<input type="hidden" name="type" value="0" />
<input type="hidden" name="program" value="20100038.TODAY" />
<input type="hidden" name="mode" value="3" />
<input type="submit" value="Post" />
</form>
--------------------
我上面的HTML的写法可行,那是因为我是用Firefox测试的可以
但是我用IE测试的时候就报
您无权查看该网页
您可能没有权限用您提供的凭据查看此目录或网页。
我想.NET连接的方式应该是IE的方式所以就出现了这个错误
是不是
那要怎么办谢谢
//构造httpwebrequest对象,注意,这里要用Create而不是new
System.Net.HttpWebRequest wReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url); //定义代理.如果是通过代理上网的则需要设定.
//System.Net.WebProxy proxy = new System.Net.WebProxy("proxyServer:intPort", true);
System.Net.WebProxy proxy = new System.Net.WebProxy("192.168.10.1", 80);
proxy.Credentials = new System.Net.NetworkCredential("cbgy10", "zhujianqi", "sipo.gov.cn");
//如果是通过代理上网的则需要设定.
wReq.Proxy = proxy; ////伪造浏览器数据,避免被防采集程序过滤
//wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215; CrazyCoder.cn;www.aub.org.cn)";
////注意,为了更全面,可以加上如下一行,避开ASP常用的POST检查
//wReq.Referer = Url;//指明来源网页,要采集页面的主页 wReq.KeepAlive = true;
wReq.ContentType = "application/x-www-form-urlencoded";
wReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
wReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"; //定义httpResponse.
System.Net.HttpWebResponse wResp = wReq.GetResponse() as System.Net.HttpWebResponse;
//定义输出流
System.IO.Stream respStream = wResp.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(respStream, encode);
string content = reader.ReadToEnd(); //close can dispose some resource.
reader.Close();
reader.Dispose();
return content;
}
}
catch (System.Exception ex)
{
return ex.Message;
}
return "";