Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么WebRequest使用无效URL?_C#_Httpwebrequest - Fatal编程技术网

C# 为什么WebRequest使用无效URL?

C# 为什么WebRequest使用无效URL?,c#,httpwebrequest,C#,Httpwebrequest,我正在尝试使用HTTPWebRequest登录到一个站点,它可以正常工作。问题是,即使我传递了错误的凭证,或者是错误的URL,它也能工作。没有抛出异常 我应该怎么做才能在下面的代码中获得异常 try { WebRequest request = WebRequest.Create("http://192.1111.1.1111"); ViewBag.Message = request.ContentLength; } catch (Exception e) { ViewB

我正在尝试使用
HTTPWebRequest
登录到一个站点,它可以正常工作。问题是,即使我传递了错误的凭证,或者是错误的URL,它也能工作。没有抛出异常

我应该怎么做才能在下面的代码中获得异常

try
{
    WebRequest request = WebRequest.Create("http://192.1111.1.1111");
    ViewBag.Message = request.ContentLength;
}
catch (Exception e)
{
    ViewBag.Message = "failed";
}

或者还有其他工作吗?

您实际上并没有提出请求,只是创建了类。为了发出请求,您需要阅读响应。您还可以通过访问请求流来启动请求

//Create a new WebRequest Object to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");

// This will throw an exception if the request fails
WebResponse myWebResponse=myWebRequest.GetResponse();

您实际上并没有发出请求,只是创建了类。为了发出请求,您需要阅读响应。您还可以通过访问请求流来启动请求

//Create a new WebRequest Object to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");

// This will throw an exception if the request fails
WebResponse myWebResponse=myWebRequest.GetResponse();

您没有使用此代码执行请求,您必须添加以下内容:

try
{
    WebRequest request = WebRequest.Create("http://192.1111.1.1111");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    ViewBag.Message = response.ContentLength;
}
catch (WebException e)
{
    ViewBag.Message = "failed";
}

之后,除例外情况外,您可以检查(401 Unauthorized等)

您没有使用此代码执行请求,您必须添加以下内容:

try
{
    WebRequest request = WebRequest.Create("http://192.1111.1.1111");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    ViewBag.Message = response.ContentLength;
}
catch (WebException e)
{
    ViewBag.Message = "failed";
}

之后,除了例外情况,您可以检查(401 Unauthorized等)

您需要实际发出请求,而不仅仅是创建类

您可以通过以下方式获得响应:


HttpWebResponse=(HttpWebResponse)request.GetResponse()

您需要实际发出请求,而不仅仅是创建类

您可以通过以下方式获得响应:


HttpWebResponse=(HttpWebResponse)request.GetResponse()

您提供的代码示例只是初始化请求对象。您提供给请求的参数被认为是有效的,因此不会引发异常

进行连接尝试时,可能会引发错误


有关什么异常以及何时引发异常的更多信息,请参阅。

您提供的代码示例只是初始化请求对象。您提供给请求的参数被认为是有效的,因此不会引发异常

进行连接尝试时,可能会引发错误


有关哪些异常以及何时出现异常的更多信息,请参阅。

谢谢大家。。。我研究了你的答案,是的,它奏效了。。。我可以用一些有效的凭证登录facebook。。。太好了!!!:)以下是我的最终代码:-

        CookieCollection cookies = new CookieCollection();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.facebook.com");
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        //Get the response from the server and save the cookies from the first request..
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        cookies = response.Cookies;



        string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
        string postData = String.Format("email={0}&pass={1}", "", "");
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(cookies); //recover cookies First request
        getRequest.Method = WebRequestMethods.Http.Post;
        getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        getRequest.AllowWriteStreamBuffering = true;
        getRequest.ProtocolVersion = HttpVersion.Version11;
        getRequest.AllowAutoRedirect = true;
        getRequest.ContentType = "application/x-www-form-urlencoded";

        byte[] byteArray = Encoding.ASCII.GetBytes(postData);
        getRequest.ContentLength = byteArray.Length;
        Stream newStream = getRequest.GetRequestStream(); //open connection
        newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
        newStream.Close();

        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            string sourceCode = sr.ReadToEnd();
            ViewBag.Message = sourceCode;

        }
当然,除了httpwebrequest和httpwebresponse之外,代码中还有很多其他内容
1) 您必须运行httpwebrequest两次,首先获取cookie集合,然后使用相同的cookiecollection实际登录,因为facebook在登录之前设置了某些cookie,登录时需要获取这些cookie…
2) (这是我前面问题的答案,即如何检查URL是否工作)因此,你要做的是获取字符串中的页面内容(在本例中为源代码)n然后只搜索一个你认为可能存在于结果页面中的特定字符串。。。就像在这种情况下,如果我成功登录,我可能会被重定向到FB上用户的主页。该页面可以有用户名或页面标题“Facebook”。。。等等。。。因此,我们如何检查有无凭据的URL的成功


就这样。。。那件简单的事几乎花了我三天的时间。。。!!!可笑

谢谢大家。。。我研究了你的答案,是的,它奏效了。。。我可以用一些有效的凭证登录facebook。。。太好了!!!:)以下是我的最终代码:-

        CookieCollection cookies = new CookieCollection();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.facebook.com");
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        //Get the response from the server and save the cookies from the first request..
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        cookies = response.Cookies;



        string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
        string postData = String.Format("email={0}&pass={1}", "", "");
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(cookies); //recover cookies First request
        getRequest.Method = WebRequestMethods.Http.Post;
        getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        getRequest.AllowWriteStreamBuffering = true;
        getRequest.ProtocolVersion = HttpVersion.Version11;
        getRequest.AllowAutoRedirect = true;
        getRequest.ContentType = "application/x-www-form-urlencoded";

        byte[] byteArray = Encoding.ASCII.GetBytes(postData);
        getRequest.ContentLength = byteArray.Length;
        Stream newStream = getRequest.GetRequestStream(); //open connection
        newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
        newStream.Close();

        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            string sourceCode = sr.ReadToEnd();
            ViewBag.Message = sourceCode;

        }
当然,除了httpwebrequest和httpwebresponse之外,代码中还有很多其他内容
1) 您必须运行httpwebrequest两次,首先获取cookie集合,然后使用相同的cookiecollection实际登录,因为facebook在登录之前设置了某些cookie,登录时需要获取这些cookie…
2) (这是我前面问题的答案,即如何检查URL是否工作)因此,你要做的是获取字符串中的页面内容(在本例中为源代码)n然后只搜索一个你认为可能存在于结果页面中的特定字符串。。。就像在这种情况下,如果我成功登录,我可能会被重定向到FB上用户的主页。该页面可以有用户名或页面标题“Facebook”。。。等等。。。因此,我们如何检查有无凭据的URL的成功


就这样。。。那件简单的事几乎花了我三天的时间。。。!!!可笑

现在我有另一个问题,你。。。我是否可以使用相同的代码进行oauth身份验证。。。。我的意思是,我有一个oauth URL,是某人给我的,还有facebook的用户名和密码。现在我尝试替换上面代码中的字符串,但没有任何用处。。。我当然会用谷歌搜索。。。它给了我所有其他的方法。。。但我想知道我是否会使用相同的代码,并进行一些修改。。。你可以建议任何网址的太,所以我可以研究那里。。。谢谢你我还有一个问题要问你。。。我是否可以使用相同的代码进行oauth身份验证。。。。我的意思是,我有一个oauth URL,是某人给我的,还有facebook的用户名和密码。现在我尝试替换上面代码中的字符串,但没有任何用处。。。我当然会用谷歌搜索。。。它给了我所有其他的方法。。。但我想知道我是否会使用相同的代码,并进行一些修改。。。你可以建议任何网址的太,所以我可以研究那里。。。提前谢谢