Webrequest和webresponse,我如何得到回复?

时间:2014-06-15 16:50:43

标签: c#

我尝试使用webrequest和webresponse将图片发布到http://uploads.im/并获得包含图像href的回复。

API(http://uploads.im/apidocs)描述如下:

The simplest example of a call. Copy this URL and open in your browser.
http://uploads.im/api?upload=http://www.google.com/images/srpr/nav_logo66.png

果然,它有效。您将被重定向到包含其他信息中的href的页面。 我尝试在我的MVC项目中重新创建这个:

public ActionResult SaveUploadedFile()
{              
       WebRequest wrq = WebRequest.Create("http://uploads.im/api?upload=http://www.google.com/images/srpr/nav_logo66.png");                    
       WebResponse wrs = wrq.GetResponse();
}

希望wrs包含与示例中页面相同的信息...当然不会......我在这里缺少什么?

编辑:这是我回来的:

enter image description here

1 个答案:

答案 0 :(得分:1)

您似乎没有阅读回复:

public ActionResult SaveUploadedFile()
{
    WebRequest wrq = WebRequest.Create("http://uploads.im/api?upload=http://www.google.com/images/srpr/nav_logo66.png");

    using (WebResponse wrs = wrq.GetResponse())
    using (Stream stream = wrs.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        string json = reader.ReadToEnd();
        // Here the json variable will contain
        // the response from the server. You could
        // parse it with a JSON parser such as JSON.NET
        // and extract the information you are looking for
    }
}