方法 : プロキシを使用して HTTP 要求を送信する

更新 : 2007 年 11 月

.NET Compact Framework では、Web サービスをサポートしています。この例では、指定されたプロキシを使用した場合と使用しない場合の、HTTP GET 要求の送信を扱います。

使用例

次のコード例では、ボタンをクリックすると、要求が送信されて応答が処理されます。この要求では、プロキシの使用が指定されていて、例外を処理する WebException クラスを使用する場合だけ、プロキシを使用します。StreamReader を使用して、HTML 応答を文字配列バッファへ読み込みます。

Private ReceiveStream As System.IO.Stream
Private sr As System.IO.StreamReader

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Get URL and proxy
    ' from the text boxes.
    Dim url As String = txtURL.Text
    Dim proxy As String = txtProxy.Text

    Try
        If Not "".Equals(txtProxy.Text) Then
            Dim proxyObject As New WebProxy(proxy, 80)

            ' Disable proxy use when the host is local.
            proxyObject.BypassProxyOnLocal = True

            ' HTTP requests use this proxy information.
            GlobalProxySelection.Select = proxyObject
        End If

        Dim req As WebRequest = WebRequest.Create(url)
        Dim result As WebResponse = req.GetResponse()
        ReceiveStream = result.GetResponseStream()
        Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
        sr = New StreamReader(ReceiveStream, encode)

        ' Read the stream into arrays of 30 characters
        ' to add as items in the list box. Repeat until
        ' buffer is read.
        Dim read(29) As [Char]
        Dim count As Integer = sr.Read(read, 0, 30)
        While count > 0
            Dim str As New String(read, 0, count)
            lstResults.Items.Add(str)
            count = sr.Read(read, 0, 30)
        End While
    Catch ex As WebException
        Dim message As String = ex.Message
        Dim response As HttpWebResponse = CType(ex.Response, HttpWebResponse)
        If Nothing Is response Then
        Else
            message = response.StatusDescription
            response.Close()
        End If
        lstResults.Items.Add(message)
    Catch ex As Exception
        lstResults.Items.Add(ex.Message)
    Finally
        ReceiveStream.Close()
        sr.Close()
    End Try
End Sub
Stream ReceiveStream = null;
StreamReader sr = null;

private void button1_Click(object sender, System.EventArgs e)
{

    // Get URL and proxy
    // from the text boxes.
    string url = txtURL.Text;
    string proxy = txtProxy.Text;

    try 
    {
        if(!"".Equals(txtProxy.Text))
        {
            WebProxy proxyObject = new WebProxy(proxy, 80);

            // Disable proxy use when the host is local.
            proxyObject.BypassProxyOnLocal = true;

            // HTTP requests use this proxy information.
            GlobalProxySelection.Select = proxyObject;

        }

        WebRequest req = WebRequest.Create(url);
        WebResponse result = req.GetResponse();
        ReceiveStream = result.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        sr = new StreamReader( ReceiveStream, encode );

        // Read the stream into arrays of 30 characters
        // to add as items in the list box. Repeat until
        // buffer is read.
        Char[] read = new Char[30];
        int count = sr.Read( read, 0, 30 );
        while (count > 0) 
        {
            String str = new String(read, 0, count);
            lstResults.Items.Add(str);
            count = sr.Read(read, 0, 30);
        }
    } 
    catch(WebException ex)
    {
       string message = ex.Message;
       HttpWebResponse response = (HttpWebResponse)ex.Response;
       if(null != response)
       {
           message = response.StatusDescription;
           response.Close();
       }
       lstResults.Items.Add(message);              
    }
    catch(Exception ex) 
    {
        lstResults.Items.Add(ex.Message);
    }
    finally
    {
        ReceiveStream.Close();
        sr.Close();
    }

}

コードのコンパイル方法

この例は、次の名前空間への参照を必要とします。

参照

その他の技術情報

.NET Compact Framework でのネットワーキングと接続性