Diễn đàn    ASP.NET & Sharepoint MOSS, WSS 2007    download content html từ URL C#

Thành viênTrả lời
awas


57  bài
21-7-2008 17:35:6
public static string GetHTMLFromURL(string url)
{
if (url.Length == 0)
throw new Exception("Invalid Url");

string html = "";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// get the response stream.
Stream responseStream = response.GetResponseStream();
// use a stream reader that understands UTF8
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
html = reader.ReadToEnd();
// close the reader
response.Close();
reader.Close();
return html;
}
 
boyvt


2  bài
30-10-2008 4:43:14
bro có thể giải thik code trên không ? mình chưa hiẻu
 
aspnet

Lập trình không biên giới
608  bài
30-10-2008 15:47:25
Cứ copy y nguyên đoạn code trên phang vào đâu đó rồi gọi ra, đầu vào cho và 1 url ví dụ http://vnexpress.net đầu ra có 1 sâu kết quả html download được chấm hết.
---
Coding for food
 
lyngochung


17  bài
4-12-2008 5:31:26
Bác ơi, em đang tìm hiểu về cái webservice. Nhưng chưa biết làm thế nào cả,
Bác có tài liệu hay biết về vấn đề này thì chỉ giúp em với. Cần những công cụ nào để thực hiện được ạ. Em cám ơn nhìu
---
 
hung


11  bài
5-12-2008 9:22:52
Bác ơi, em đang tìm hiểu về cái webservice. Nhưng chưa biết làm thế nào cả,
Bác có tài liệu hay biết về vấn đề này thì chỉ giúp em với. Cần những công cụ nào để thực hiện được ạ. Em cám ơn nhìu
---



using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// Summary description for ws
/// </summary>
[WebService(Namespace = "http://awas.vn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ws : System.Web.Services.WebService
{

public ws()
{

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public int Add(int a, int b)
{
return (a + b);
}

}





Viết đoạn code này vứt vào file ws.asmx trong 1 cái web project nào đó. Coi nó là server. Còn client thì add cái web reference vào



Nó sẽ ra cái hình như sau:



Nhớ bấm và cái Add Reference nhé.

Rõ ràng chúng ta nhìn thấy cái hàm add mới viết. Và trong code tại client chúng ta cứ thế mà gọi ra thôi.
 
lyngochung


17  bài
6-12-2008 2:4:18
Dạ em cám ơn bác nhìu.
---
 
lyngochung


17  bài
6-12-2008 2:11:36
Bác cho em hỏi 1 câu nữa với. em có một đoạn code webservice như sau:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class myService : System.Web.Services.WebService
{
public string myURL;
public myService()
{
}
public myService(string URL)
{
this.myURL = URL;
}
[WebMethod]
public string mGetHTML()
{
string sHTML = "";
WebRequest oRequest = WebRequest.Create(myURL);
oRequest.ContentLength = 0;
WebResponse oResponse = oRequest.GetResponse();
StreamReader oStreamReader = new StreamReader(oResponse.GetResponseStream());
sHTML = oStreamReader.ReadToEnd();
oStreamReader.Close();
return sHTML;
}
}
Em gọi webservice này từ winform: nhưng khi em gởi tạo lớp service thi nó chỉ có phương thức khởi tạo không có tham số. Với lại thuộc tính myURL em để public nhưng bên winform cũng không truy cập được. Mong sự giúp đỡ của các bác!
---
 
lyngochung


17  bài
6-12-2008 2:22:58
Hihi, em đang nghiên cứ về xây dựng dịch vụ thu thập tin tức trực tuyến từ các website trên internet. Bác nào có kinh nghiệm liên quan đến vấn đề này xin đuọc chỉ dạy em với.
Bước 1: tìm hiểu webservice
Bước 2: thu thập thông tin
Bước 3: phân loại thông tin, và lưu vào database
Bước 4: xây dựng website hiển thị thông tin

Bác nào thành thạo về vấn đề nào thì giúp em với. em xin cảm ơn và hậu tạ.
---
 
awas


57  bài
6-12-2008 19:10:34
Em gọi webservice này từ winform: nhưng khi em gởi tạo lớp service thi nó chỉ có phương thức khởi tạo không có tham số. Với lại thuộc tính myURL em để public nhưng bên winform cũng không truy cập được. Mong sự giúp đỡ của các bác!


cái này ai lại làm thế này. Dùng

string mGetHTML(string url)
{
downloadHtml down = new downloadHtml();
return down.mGetHTML(url);
}

Còn lại trong downloadHtml thích viết cái gì thì viết. Đơn giản chỉ là vấn đề tổ chức dữ liệu thôi mà.
---
 
awas


57  bài
14-3-2010 11:50:56
Có một cách khác để download nội dung html từ 1 url xác định trước


string url = "http://vnexpress.net/GL/Home/";
System.Net.WebClient client = new System.Net.WebClient();
System.IO.Stream stm = client.OpenRead(url);
System.IO.StreamReader reader = new System.IO.StreamReader(stm);
string htm = reader.ReadToEnd();
 
aspnet

Lập trình không biên giới
608  bài
7-5-2022 9:28:16
[code]
  Regex regex = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase);
MatchCollection urls = regex.Matches(html);
for(int i = 0; i < urls.Count; i++)
{
Response.Write(urls.ToString());
}

[/code]
Tim tat ca herf
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM