Visual Studio 2010 と 2012 / WebClient と HttpClient
日時: 2013/04/21 02:52
名前: lightbox
両方とも コンソールアプリケーションは WebClient
拡張子:
using System;
//using System.Collections.Generic;//using System.Linq;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var wc = new WebClient();
wc.Encoding = System.Text.Encoding.GetEncoding("UTF-8");
string web_text = wc.DownloadString("http://textt.net/sworc/20121226075830.txt");
wc.Dispose();
Console.WriteLine(web_text);
Console.ReadLine();
}
}
}
通常 Windows アプリケーション で HttpClient
拡張子:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
var hc = new HttpClient();
hc.MaxResponseContentBufferSize = 256000;
try
{
HttpResponseMessage response = await hc.GetAsync("http://winofsql.jp");
response.EnsureSuccessStatusCode();
// var responseBodyAsText = await response.Content.ReadAsStringAsync();
var data = await response.Content.ReadAsByteArrayAsync();
var web_text = Encoding.GetEncoding("SHIFT_JIS").GetString(data);
Debug.WriteLine(web_text);
}
catch (HttpRequestException hre)
{
Debug.WriteLine(hre.ToString());
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
hc.Dispose();
}
}
}