简体   繁体   English

如何获取Web服务的GET请求的响应并将其存储在变量中?

[英]How to get response of GET request of web service and store it in a variable?

In my code I am sending a GET request to a server. 在我的代码中,我正在向服务器发送GET请求。 In response of this I will get one URL which I want to store in a variable. 作为响应,我将获得一个要存储在变量中的URL。

Can anyone help me regarding this? 有人可以帮我吗? My code so far: 到目前为止,我的代码:

private void CreatePublicUrl()
{
    String CreatePublicUrl = String.Format("{0}/DataObjectServer/data/do/getproperties?cat=do&key=baseXmlPath&t={1}", base_url.Value, token);
    Debug.WriteLine("CreatePublicUrl==>" + CreatePublicUrl);
    HttpSyncRequest pub_url = new HttpSyncRequest();

    pub_url.sendGet(CreatePublicUrl, (urlResp) =>
    {
        var url = new Uri(urlResp);
        //      String urlresponse = JsonConvert.urlResp;
    });
}

It seem you're trying to achieve it using some proprietary library. 看来您正在尝试使用某些专有库来实现它。 You can achieve the same using HttpWebRequest and WebResponse . 您可以使用HttpWebRequestWebResponse实现相同的目的。

You can also do it using WebClient class as follows 您也可以使用WebClient类,如下所示

    WebClient client = new WebClient();
    String CreatePublicUrl = String.Format("{0}/DataObjectServer/data/do/getproperties?cat=do&key=baseXmlPath&t={1}", base_url.Value, token);
    using (Stream data = client.OpenRead(CreatePublicUrl))
    {
        using (StreamReader reader = new StreamReader(data))
        {
            string content = reader.ReadToEnd();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM