私は自分のアプリケーションの一つに、このメソッドを使用します。
private static string RetrieveData(string url)
{
// used to build entire input
var sb = new StringBuilder();
// used on each read operation
var buf = new byte[8192];
try
{
// prepare the web page we will be asking for
var request = (HttpWebRequest)
WebRequest.Create(url);
/* Using the proxy class to access the site
* Uri proxyURI = new Uri("http://proxy.com:80");
request.Proxy = new WebProxy(proxyURI);
request.Proxy.Credentials = new NetworkCredential("proxyuser", "proxypassword");*/
// execute the request
var response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
} while (count > 0); // any more data to read?
}
catch(Exception exception)
{
MessageBox.Show(@"Failed to retrieve data from the network. Please check you internet connection: " +
exception);
}
return sb.ToString();
}
あなたはちょうどあなたがコードを取得する必要のあるWebページのURLを渡す必要があります。例えば
:
string htmlSourceGoggle = RetrieveData("www.google.com")
注:あなたがインターネットへのアクセスにプロキシを使用する場合は、プロキシ設定をアンコメント取得することができます。プロキシアドレス、ユーザー名、およびパスワードを、使用しているものと置き換えます。
コードによるログイン。これを確認してください:Login to website, via C#
これは、これは私が最初に働いていたURLに基づいてソースを取得するために働いています。私のサイトでは特定のページを表示するためにログインする必要があるため(たとえば、クエリ文字列にIDを持つページを指定するなど)、常にログインページのソースを取得します。そのページには、ログインしていないURLのすぐ上に、あなたを放置することはありません。これについて何をすべきか、私ができることが何かあるかどうかはわかりません。 – Drew
@Drewリンクで回答を更新しました – reggie
http://stackoverflow.com/questions/930807/c-sharp-login-to-website-via-program/931030#931030 – reggie