/// <summary>
/// 获取远程html
/// </summary>
/// <param name="url"></param>
/// <param name="methed"></param>
/// <param name="param"></param>
/// <param name="html"></param>
/// <returns></returns>
public static bool GetHttp(string url, string methed, string param, out string html)
{
methed = methed.ToLower(); if (param != null && methed == "get" && param.Length > 0)
{
url += "?" + param;
} try
{
MSXML2.XMLHTTP mx = new MSXML2.XMLHTTPClass(); mx.open(methed, url, false, null, null); if (param != null && methed == "post" && param.Length > 0)
{
mx.setRequestHeader("Content-Length", param.Length.ToString());
mx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
} mx.send(param); if (mx.readyState != 4)
{
html = "远程连接失败:-4";
return false;
}
html = mx.responseText;
return true;
}
catch (Exception ex)
{
html = "远程连接失败:"+ex.Message;
return false;
}
} public static bool GetHttp1(string url, string methed, string param, string referer, string encode, out string html)
{
//return GetHttp(url,methed,param,out html); //string encode = "utf-8";
//string methed = sendType.ToString(); if (param != null && methed == "get" && param.Length > 0)
{
if (url.IndexOf("?") >= 0)
{
url += "&" + param;
}
else
{
url += "?" + param;
}
} try
{
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url); webreq.Proxy=null;
webreq.Timeout = 1000 * 6;
webreq.ContentType = "application/x-www-form-urlencoded";
webreq.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0"; //webreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.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)"; //谷歌的:User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
//火狐的:User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
//标准格式为: 浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息 //webreq.AllowAutoRedirect = false; //频繁请求一个网址时,过段时间就会出现“基础连接已经关闭”
//webreq.KeepAlive = false;
//webreq.ProtocolVersion = HttpVersion.Version10; if (referer.Length > 0)
{
webreq.Referer = referer;
} CookieContainer mycookies = new CookieContainer();
webreq.CookieContainer = mycookies; //if (this.cookieList != null)
//{
// webreq.CookieContainer.Add(this.GetCookies(webreq.RequestUri, this.cookieList));
//} webreq.Method = methed; //post 开始
if (param != null && methed == "post")
{
byte[] arrbyte = Encoding.GetEncoding(encode).GetBytes(param);
webreq.ContentLength = arrbyte.Length; Stream newStream = webreq.GetRequestStream();
newStream.Write(arrbyte, 0, arrbyte.Length);
newStream.Close();
}
//post 结束 WebResponse w = webreq.GetResponse(); //返回HTML
using (HttpWebResponse webres = (HttpWebResponse)webreq.GetResponse())
{
using (Stream dataStream = webres.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding(encode)))
{
html = reader.ReadToEnd();
//this.cookieList = webreq.CookieContainer.GetCookies(webreq.RequestUri);
webreq.Abort();//可能会解决卡住或阻塞问题
}
}
}
}
catch (Exception ex)
{ html = "出现异常(HttpHelper.GetHTML),远程连接失败:" + ex.Message + " url:" + url;
//System.Windows.Forms.MessageBox.Show(html);
return false;
} return true;
}

  

获取远程html的更多相关文章

  1. 使用JCIFS获取远程共享文件

    package com.jadyer.util;  import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...

  2. curl获取远程图片存到本地

    $url = 'http://sssss/sss/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic ...

  3. php 获取远程图片保存到本地

    php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...

  4. 解析PHP中的file_get_contents获取远程页面乱码的问题【转】

    在工作中,遇到一个问题.我需要将一个网址(该网址是一个json数据的接口,即 打开该网址,在浏览器中显示的是json数据),我使用file_get_contents($url),数据是乱码的. 通过查 ...

  5. php获取远程文件大小

    获取本地文件大小filesize()就可以了,但是如何获取远程文件的大小呢? 这里介绍三个方法来获取远程文件的大小. 方法1:get_headers <?php get_headers($url ...

  6. scp命令获取远程文件

    一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响 ...

  7. PHP高效获取远程图片尺寸和大小(转)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. php 获取远程图片

    一 function gethttpimage($url){      set_time_limit(0);      if(!empty($url)){        $imgUrl=date('Y ...

  9. C# 之 服务端获取远程资源

    获取指定网页的远程资源可使用 WebClient.WebRequest.HttpWebRequest 三种方式来实现.当然也可使用webBrowse,webBrowse不做介绍. 通过 System. ...

  10. Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案

    本人在做百度账户第三方登录接口,获取百度token,利用php操作curl post方式发送请求token,出现couldn't connect to host错误.经过调试测试,最后终于成功.回头写 ...

随机推荐

  1. 21.SQL to MongoDB Mapping Chart-官方文档摘录

    有关关系型数据库跟Mongod的语法对比 In addition to the charts that follow, you might want to consider the Frequentl ...

  2. Python 之父谈放弃 Python:我对核心成员们失望至极!

    Python 之父讲述退位原因,以及 Python 的未来将何去何从. ​ 在 Python 社区,Python 的发明者 Guido Van Rossum 被称为 “仁慈的终生独裁者”(BDFL,B ...

  3. SpringBean 定义继承

    Bean定义继承 bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等.子bean的定义继承副定义的配置数据.子定义可以根据需要重写一些值 ...

  4. python web框架 django 练习1 django 1.11版本

    django练习 在我自己项目里创建一个xiaoliu的文件夹 里面创建s1.py 文件 s1.py文件 里面写各种函数 from django.shortcuts import HttpRespon ...

  5. .NET加密方式解析--散列加密

    在现代社会中,信息安全对于每一个人都是至关重要的,例如我们的银行账户安全.支付宝和微信账户安全.以及邮箱等等,说到信息安全,那就必须得提到加密技术,至于加密的一些相关概念,在这里就不说了. 这一次将会 ...

  6. java-mybaits-00301-SqlMapConfig

    1.配置内容 mybatis的全局配置文件SqlMapConfig.xml,SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) t ...

  7. 文件传输(xmodem协议)

    https://www.menie.org/georges/embedded/ 需要移植如下两个基础的硬件读写函数 int _inbyte(unsigned short timeout); void ...

  8. Spring boot 打包瘦身方法

    背景 随着spring boot 的流行.越来越多的来发着选择使用spring boot 来发 web 应用. 不同于传统的 web 应用 需要 war 包来发布应用. spring boot 应用可 ...

  9. linux文件编程----系统调用

    linux中文件编程可以使用两种方法: 1).linux系统调用 2).C语言库函数 前者依赖于linux系统,后者与操作系统是独立的. 在 linux系统中,所有打开的文件也对应一个数字,这个数字由 ...

  10. (转)extern关键字两种场景的使用

    第一种场景 -- extern extern关键字的作用是声明变量和函数为外部链接,即该变量或函数名在其它文件中可见.用其声明的变量或函数应该在别的文件或同一文件的其它地方定义. 例如语句:exter ...