Showing posts with label dataset. Show all posts
Showing posts with label dataset. Show all posts

Saturday, March 24, 2012

System.Net.Webclient screen scraping: how to gracefully handle 403 (and other) errors?

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs. It
worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.

I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next ID
in the dataset rather than aborting the whole job.

My code appears below. Thank you in advance.

-KF

string strConnection;

strConnection = ConfigurationSettings.AppSettings["connwhatever"];

SqlConnection conn = new SqlConnection(strConnection);

string query = // [my query];

SqlDataAdapter a = new SqlDataAdapter(query, conn);

DataSet s = new DataSet();

a.Fill(s);

int counter = 0;

foreach (DataRow dr in s.Tables[0].Rows)

{

counter++;

System.Net.WebClient wc = new WebClient();

string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());

FileStream fstream = new FileStream(@dotnet.itags.org."c:\whateverpath\" + dr[0].ToString() +
".htm", FileMode.Create, FileAccess.Write);

StreamWriter stream = new StreamWriter(fstream);

stream.Write(strData);

stream.Close();

fstream.Close();please read chapter on try/catch

-- bruce (sqlwork.com)

kenfine@.nospam.nospam wrote:

Quote:

Originally Posted by

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs. It
worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.
>
I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next ID
in the dataset rather than aborting the whole job.
>
My code appears below. Thank you in advance.
>
-KF
>
string strConnection;
>
strConnection = ConfigurationSettings.AppSettings["connwhatever"];
>
SqlConnection conn = new SqlConnection(strConnection);
>
string query = // [my query];
>
SqlDataAdapter a = new SqlDataAdapter(query, conn);
>
DataSet s = new DataSet();
>
a.Fill(s);
>
int counter = 0;
>
foreach (DataRow dr in s.Tables[0].Rows)
>
{
>
counter++;
>
System.Net.WebClient wc = new WebClient();
>
string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());
>
FileStream fstream = new FileStream(@."c:\whateverpath\" + dr[0].ToString() +
".htm", FileMode.Create, FileAccess.Write);
>
StreamWriter stream = new StreamWriter(fstream);
>
stream.Write(strData);
>
stream.Close();
>
fstream.Close();
>
>
>
>


Understand try/catch generally. What event(s) should I be trying to catch?

Thank you,
-KF

"bruce barker" <nospam@.nospam.comwrote in message
news:%23F79bSINHHA.3288@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

please read chapter on try/catch
>
-- bruce (sqlwork.com)
>
kenfine@.nospam.nospam wrote:

Quote:

Originally Posted by

>I've written a very small ASP.NET page to scrape thousands of pages of
>content based on database IDs. It loops through a dataset to get the IDs.
>It worked well in testing but now I am getting an annoying 403 error that
>causes the script to abort halfway through my download.
>>
>I am wondering if there is a way in ASP.NET to have my code ignore 403
>errors and other network errors, catch the error, and iterate to the next
>ID in the dataset rather than aborting the whole job.
>>
>My code appears below. Thank you in advance.
>>
>-KF
>>
>string strConnection;
>>
>strConnection = ConfigurationSettings.AppSettings["connwhatever"];
>>
>SqlConnection conn = new SqlConnection(strConnection);
>>
>string query = // [my query];
>>
>SqlDataAdapter a = new SqlDataAdapter(query, conn);
>>
>DataSet s = new DataSet();
>>
>a.Fill(s);
>>
>int counter = 0;
>>
>foreach (DataRow dr in s.Tables[0].Rows)
>>
>{
>>
>counter++;
>>
>System.Net.WebClient wc = new WebClient();
>>
>string strData =
>wc.DownloadString("http://whatever.org/article.asp?articleid=" +
>dr[0].ToString());
>>
>FileStream fstream = new FileStream(@."c:\whateverpath\" +
>dr[0].ToString() + ".htm", FileMode.Create, FileAccess.Write);
>>
>StreamWriter stream = new StreamWriter(fstream);
>>
>stream.Write(strData);
>>
>stream.Close();
>>
>fstream.Close();
>>
>>
>>


Hello KF,

Based on your description, you're using the webclient class to request many
web pages programmatically in ASP.NET page code. However, since some page
may raise some exception, your client loop code in ASP.NET page break,
correct?

As for the 403 error, it is normally caused by the security authorization
checking at server-side fails. I'm not sure whether there is any other
particular scenario here, however, if what you want is simply captuer and
ignore such error and continue the loop, you can just add a try catch block
around your webclient class's downloadXXX method call and if any exception
captured you can simply ignore it and skip the current loop. e.g.

=======================
foreach (DataRow dr in s.Tables[0].Rows)

{

counter++;

System.Net.WebClient wc = new WebClient();

try
{

string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());

}catch(Exception ex)
{
//ignore and continue the loop
}

...........................

}
=========================

Does this work for your scenario?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
For webclient or HttpWebRequest, it normally will throw a
System.Net.WebException, however, any exception can be handled by the super
class "Exception". So you can use either

try
{
}catch(Exception)
{

}

or

try
{
}catch(WebException)
{

}

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
You can specify error documents for specific error-codes.

In your web.config, add the following entries in <system.websection:

<customErrors>
<error statusCode="403" redirect="403.aspx"/>
</customErrors>

Note that generally, 403 would be given by the web server and not be the
ASP.Net engine. At times, when the authentication fails, 403 may be returned
by an IHttpModule - like the authentication modules (NTML, Kerberos, Digest
etc).

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------

<kenfine@.nospam.nospamwrote in message
news:%23SginPGNHHA.4916@.TK2MSFTNGP06.phx.gbl...

Quote:

Originally Posted by

I've written a very small ASP.NET page to scrape thousands of pages of
content based on database IDs. It loops through a dataset to get the IDs.
It worked well in testing but now I am getting an annoying 403 error that
causes the script to abort halfway through my download.
>
I am wondering if there is a way in ASP.NET to have my code ignore 403
errors and other network errors, catch the error, and iterate to the next
ID in the dataset rather than aborting the whole job.
>
My code appears below. Thank you in advance.


This worked great for my scenario. Thanks very much to everyone for the
timely assistance.

-KF

"Steven Cheng[MSFT]" <stcheng@.online.microsoft.comwrote in message
news:FYKH6FJNHHA.2024@.TK2MSFTNGHUB02.phx.gbl...

Quote:

Originally Posted by

Hello KF,
>
Based on your description, you're using the webclient class to request
many
web pages programmatically in ASP.NET page code. However, since some page
may raise some exception, your client loop code in ASP.NET page break,
correct?
>
As for the 403 error, it is normally caused by the security authorization
checking at server-side fails. I'm not sure whether there is any other
particular scenario here, however, if what you want is simply captuer and
ignore such error and continue the loop, you can just add a try catch
block
around your webclient class's downloadXXX method call and if any exception
captured you can simply ignore it and skip the current loop. e.g.
>
=======================
foreach (DataRow dr in s.Tables[0].Rows)
>
{
>
counter++;
>
System.Net.WebClient wc = new WebClient();
>
try
{
>
string strData =
wc.DownloadString("http://whatever.org/article.asp?articleid=" +
dr[0].ToString());
>
}catch(Exception ex)
{
//ignore and continue the loop
}
>
...........................
>
}
=========================
>
Does this work for your scenario?
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
>
==================================================
>
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.
>
>
>
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.
>
==================================================
>
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>

Thursday, March 22, 2012

System.OutOfMemoryException

What should I do to combat this error? I am doing fairly heavy dataset caching on my webapp. I have 2 gigs memory, and aspwp.exe is using up to 1.3gigs. This caching is essential to my application as it saves ALOT of database queries.

I thought asp.net would recycle what it needed to. Is the best solution to just thro more RAM at it? Thats what I plan on doing, but its going to take a few days to get the server up and running and need a solution in the meantime. Also I am worried what is going to happen when I have my server maxed out on ram usage and I get this error

thanks for any help

mike123Are you setting any priority on the cache items when adding them? Reason I ask is that if everything is being set to "high", it may not be recycling anything w/ the garbage collection.

Also, when you add things to cache, are you checking to see if they already exist or possibly even deleting the old cache before adding it again?
Are any of your caches set to time out (I would think so).

If your cache is too broad, I can see this being a problem. If you are caching a search page, it would be very possible for you to hit the limit of your RAM.

Charley.
I am not setting priority on anything, not high or low. Here is an example of how I cache my dataset. I'm not sure about deleting the old cache before adding it again, because I dont think that happens since I check before I add, please correct me if im wrong on that.

Also, Charley I believe you may be exactly correct on the searches taking all of my memory, I am looking at making them expire quicker and setting their priority low. Is this what you would recommend? How would you modify this function?

Thanks alot
mike123

Public Function QuestionsGet(ByVal userID As Integer) As DataSet

Dim myDataSet As New DataSet()

If HttpContext.Current.Cache.Get("select_questions" & CStr(userID)) Is Nothing Then

' Create Instance of Connection and Command Object
Dim objConn As New SqlConnection("uid=")
Dim objCommand As New SqlDataAdapter("select_questions", objConn)

' Mark the Command as a SPROC
objCommand.SelectCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim parameterUserID As New SqlParameter("@.userID", SqlDbType.Int, 4)
parameterUserID.Value = userID
objCommand.SelectCommand.Parameters.Add(parameterUserID)

'Fill the DataSet
objCommand.Fill(myDataSet, "select_questions")
HttpContext.Current.Cache.Item("select_questions" & CStr(userID)) = myDataSet

Else
myDataSet = CType(HttpContext.Current.Cache.Get("select_questions" & CStr(userID)), DataSet)
End If

Return myDataSet

End Function
Tell us a bit about what you are trying to accomplish.

What type of data are you returning?

I see 'questions' based by the user Id.

Are these very static?

If so, a different way to approch this might be to generate an XML file durning a nightly batch, and instead of reading the DB, read that file instead...
They are fairly static I have probably 20k cached, but not static enough to do a nightly update I believe. I have at least a few hundred changing daily. They are directly tied to users info, I have cached each users info. The data is about 8 text strings shorter than 500 chars.

Thanks for the suggestion, but I dont think it will work in this case. What do you think?

Also, I am attempting to add ", Caching.CacheItemPriority.Low" to my cache statement, but its not accepting the final argument. Am I approaching this wrong?

Thanks again
mike123
anyone? I'm still getting this error and have no idea what to do! :(

thanks again
mike123
Mike.

I'm just throwing out ideas here...

Instead of caching the datasets, have you tried caching the page itself?

Some other options are (Not saying these are the BEST options, but options nonetheless):
- Disable caching until you get more RAM
- Reevaluate queries and see if you can combine anything
- Reevaluate your architecture so that you aren't as database intensive on any given page

Are your pages hitting directly against the database?
Charley,

Unforunately I can't output cache the page, because I need to manually expire it when certain occurances happen. One page in particular has 4 queries, and is being hit about 1 million times per day, so if I lose the caching my database server is going to have soem seroius extra load on it. I would love to get more RAM, however I need a scalable solution and I dont want the same thing to happen when I max out on my new server with 4gigs instead of 2.

Thanks for the input

mike123
4 million queries a day... yowsers...

What the heck kind of app is this?

Have you done performance monitoring on the SQL server?

If you have caching off, does it peg that server?
It's a web app where users have their own personal page, that is database driven. Yes, it sure does peg the server if I turn caching off so I really appreciate the caching. It just really sucks to have it crap out when it runs out of memory, I thought asp.net would be a little better at handling it.

I can upgrade to 4 gigs, but thats just a temporary solution and its goign to cost alot of time / money.

Thanks