/****************************************************************************
* Copyright (C) 2008 Peter Mortensen and Matthias Mann *
* This file is part of MSQuant. *
* *
* MSQuant is distributed under the terms of *
* the GNU General Public License. See src/COPYING.TXT or *
* <http://www.gnu.org/licenses/gpl.txt> for details. *
* *
* MSQuant is free software; you can redistribute it *
* and/or modify it under the terms of the GNU *
* General Public License as published by the Free *
* Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* MSQuant is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the *
* implied warranty of MERCHANTABILITY or FITNESS FOR *
* A PARTICULAR PURPOSE. See the GNU General Public *
* License for more details. *
* *
* You should have received a copy of the GNU General *
* Public License along with MSQuant; if not, write to *
* the Free Software Foundation, Inc., 59 Temple *
* Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* Purpose: our version of WebClient.DownloadFile() with a timeout *
* and others. *
* *
****************************************************************************/
/****************************************************************************
* CEBI *
* Software Development Group *
* Peter Mortensen *
* E-mail: NUKESPAMMERSdrmortensen@get2netZZZZZZ.dk *
* WWW: http://www.cebi.sdu.dk/ *
* *
* Program for post-processing of result from search in mass *
* spectrometric data. *
* *
* FILENAME: isotopeDistribution.cs *
* TYPE: CSHARP *
* *
* CREATED: PM 2008-01-24 Vrs 1.0. *
* UPDATED: PM 2008-xx-xx *
* *
* *
****************************************************************************/
//using System;
//using System.Collections.Generic;
//using System.Text;
using System.Net; //For HttpWebRequest.
using System.IO; //For Stream.
using SDUPutility; //PILInputOutput
public class SDUPinternetUtilities
{
//This is our version of System.Net.WebClient.DownloadFile() with
//a user-definable timeout...
public static void SDUP_DownloadFile(
string anAddress, string anOutputFileName, double aTimeOutSeconds)
{
//System.Net.WebClient wc;
//wc.DownloadFile(string address, string fileName)
////Our getVars, to test the get of our php. We can get a page
////without any of these vars too though.
//string getVars = "?var1=test1&var2=test2";
////Initialisation, we use localhost, change if appliable
//HttpWebRequest WebReq =
// (HttpWebRequest)WebRequest.Create(string.Format("http://127.0.0.1/test.php{0}", getVars));
HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create(anAddress);
//This time, our method is GET.
WebReq.Method = "GET";
double timeOutmsecs = 1000.0 * aTimeOutSeconds;
WebReq.Timeout = (int)timeOutmsecs;
//From here on, it's all the same as above.
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
////Let's show some information about the response
//Console.WriteLine(WebResp.StatusCode);
//Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream AnswerStream = WebResp.GetResponseStream();
StreamReader AnswerSR = new StreamReader(AnswerStream);
//Really bad idea!!! This loads the entire into memory, a single string...
//In the application we used it for retrieving protein sequences from
//the Mascot (an unreliable way in itself). But used in MRL it failed
//with a 285 MB file.
//
//Perhaps use/implement/copy/transform this from the MRL project:
// pushStreamToFile(). It works!
//
string toOutput = AnswerSR.ReadToEnd();
int len = toOutput.Length;
PILInputOutput.pushToFile(anOutputFileName, toOutput);
AnswerSR.Close();
AnswerStream.Close(); //Needed??
WebResp.Close(); //Needed??
//Congratulations, with these two functions in basic form, you
//just learned the two basic forms of web surfing
//This proves how easy it can be.
} //SDUP_DownloadFile()
} //class SDUPinternetUtilities
Generated by script codePublish.pl at 2009-01-05T15:20:59.