简体   繁体   English

在尝试从Web API获取JSON数据的教程时,我偶然发现了此错误,而视频没有遇到此错误

[英]Trying out a tutorial for getting JSON data from a web API and I stumbled on this error while the video did not encounter this error

All it tells me to do is change GetResponse to either: EndGetResponse(), BeginGetResponse() or GetResponseAsync() 它告诉我要做的就是将GetResponse更改为:EndGetResponse(),BeginGetResponse()或GetResponseAsync()

The error message reads: 'HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing an assembly reference?) 错误消息显示:'HttpWebRequest'不包含'GetResponse'的定义,并且找不到扩展方法'GetResponse'接受类型为'HttpWebRequest'的第一个参数(您是否缺少程序集引用?)

and I'm getting an error on this specific section: 我在此特定部分遇到错误:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if(response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("error code:" + response.StatusCode.ToString());

This was the code in said video: 这是所述视频中的代码:

public enum httpVerb
{
    GET,
    POST,
    PUT,
    DELETE
}

class RestClient
{
    public string endPoint { get; set; }
    public httpVerb httpMethod { get; set; }

    public RestClient()
    {
        endPoint = string.Empty;
        httpMethod = httpVerb.GET;
    }

    public string makeRequest()
    {
        string strResponseValue = string.Empty;

        HttpWebRequest request =      (HttpWebRequest)WebRequest.Create(endPoint);

        request.Method = httpMethod.ToString();

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if(response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("error code:" + response.StatusCode.ToString());
            }

            using (Stream responseStream = response.GetResponseStream())
            {
                if(responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponseValue = reader.ReadToEnd();
                    }
                }
            }
        }

        return strResponseValue;
    }
}

} }

Without knowing the specifics of the video or what the rest of your code/project look like I can only go by the error: 在不知道视频细节或您的代码/项目其余部分是什么样的情况下,我只能绕过错误:

HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing an assembly reference?)

Have you checked your assembly references to see if you have a reference to System.Net? 您是否检查了程序集引用以查看是否对System.Net进行了引用?

EDIT 编辑

It seems like you might be confusing using statements with assembly references. 看起来您可能会混淆将语句与程序集引用一起使用。 Although your class file contains using statements to System.Net, your project itself might not have a reference to the System.Net dll. 尽管您的类文件包含对System.Net的using语句,但是您的项目本身可能没有对System.Net dll的引用。

Here is a quick guide on how to manage those references in Visual Studio. 是有关如何在Visual Studio中管理这些引用的快速指南。

As a quick check: 快速检查:

  1. Find the References section in your Visual Studio project. 在您的Visual Studio项目中找到“ References部分。
  2. Right Click `References' 右键单击“参考”
  3. Click Add Reference 点击Add Reference
  4. In the Assemblies > Framework section verify that System.Net is checked Assemblies > Framework部分验证System.Net检查

EDIT #2 编辑#2

It looks like you created a UWP project instead of the video's perscribed Windows Form project type. 好像您创建了UWP项目,而不是视频指定的Windows Form项目类型。 The reason you are getting errors in your code is because the GetResponse() method of HttpWebRequest cannot be used in UWP projects. 代码出错的原因是因为HttpWebRequest的GetResponse()方法无法在UWP项目中使用。 This answer explains in a bit more detail. 答案将更详细地说明。

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

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