66

HTTPget リクエストでパラメータを渡すことはできますか? もしそうなら、どうすればいいですか?HTTP投稿リクエスト (リンク)を見つけました。この例では、文字列postDataが Web サーバーに送信されます。代わりにgetを使用して同じことをしたいと思います。Google はHTTPget here でこの例を見つけました。ただし、パラメーターは Web サーバーに送信されません。

4

5 に答える 5

145

私の好みの方法はこれです。エスケープと解析を処理します。

WebClient webClient = new WebClient();
webClient.QueryString.Add("param1", "value1");
webClient.QueryString.Add("param2", "value2");
string result = webClient.DownloadString("http://theurl.com");
于 2013-06-20T16:59:18.807 に答える
97

最初WebClientは使いやすいです。GET引数はクエリ文字列で指定されます-唯一のトリックは、値をエスケープすることを忘れないことです。

        string address = string.Format(
            "http://foobar/somepage?arg1={0}&arg2={1}",
            Uri.EscapeDataString("escape me"),
            Uri.EscapeDataString("& me !!"));
        string text;
        using (WebClient client = new WebClient())
        {
            text = client.DownloadString(address);
        }
于 2009-02-05T07:27:08.210 に答える
39

GETリクエストでは、クエリ文字列の一部としてパラメータを渡します。

string url = "http://somesite.com?var=12345";
于 2009-02-05T07:20:53.433 に答える
8

WebRequest オブジェクトは、私には負担が大きすぎるようです。私は WebClient コントロールを使用することを好みます。

この関数を使用するには、パラメーターと要求ヘッダーを保持する 2 つの NameValueCollection を作成するだけです。

次の関数を検討してください。

    private static string DoGET(string URL,NameValueCollection QueryStringParameters = null, NameValueCollection RequestHeaders = null)
    {
        string ResponseText = null;
        using (WebClient client = new WebClient())
        {
            try
            {
                if (RequestHeaders != null)
                {
                    if (RequestHeaders.Count > 0)
                    {
                        foreach (string header in RequestHeaders.AllKeys)
                            client.Headers.Add(header, RequestHeaders[header]);
                    }
                }
                if (QueryStringParameters != null)
                {
                    if (QueryStringParameters.Count > 0)
                    {
                        foreach (string parm in QueryStringParameters.AllKeys)
                            client.QueryString.Add(parm, QueryStringParameters[parm]);
                    }
                }
                byte[] ResponseBytes = client.DownloadData(URL);
                ResponseText = Encoding.UTF8.GetString(ResponseBytes);
            }
            catch (WebException exception)
            {
                if (exception.Response != null)
                {
                    var responseStream = exception.Response.GetResponseStream();

                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            Response.Write(reader.ReadToEnd());
                        }
                    }
                }
            }
        }
        return ResponseText;
    }

クエリ文字列パラメーター (必要な場合) を NameValueCollection として追加します。

        NameValueCollection QueryStringParameters = new NameValueCollection();
        QueryStringParameters.Add("id", "123");
        QueryStringParameters.Add("category", "A");

http ヘッダー (必要な場合) を NameValueCollection として追加します。

        NameValueCollection RequestHttpHeaders = new NameValueCollection();
        RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");
于 2014-07-09T15:51:54.520 に答える