Share via


方法 : プロキシで、HTTP 要求を送信します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework は、Web サービスをサポートします。 次の使用例は、指定したプロキシの有無、HTTP GET 要求を送信します。

使用例

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

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

PrivateSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click

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

    TryIfNot"".Equals(txtProxy.Text) ThenDim proxyObject AsNew WebProxy(proxy, 80)

            ' Disable proxy use when the host is local.
            proxyObject.BypassProxyOnLocal = True
            ' HTTP requests use this proxy information.
            GlobalProxySelection.Select = proxyObject
        EndIfDim 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 AsInteger = sr.Read(read, 0, 30)
        While count > 0
            Dim str AsNewString(read, 0, count)
            lstResults.Items.Add(str)
            count = sr.Read(read, 0, 30)
        EndWhileCatch ex As WebException
        Dim message AsString = ex.Message
        Dim response As HttpWebResponse = CType(ex.Response, HttpWebResponse)
        IfNothingIs response ThenElse
            message = response.StatusDescription
            response.Close()
        EndIf
        lstResults.Items.Add(message)
    Catch ex As Exception
        lstResults.Items.Add(ex.Message)
    Finally
        ReceiveStream.Close()
        sr.Close()
    EndTryEndSub
Stream ReceiveStream = null;
StreamReader sr = null;

privatevoid 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 での接続の圧縮フレームワーク