Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# 如何使用c将搜索字符串发布到google.com?_C#_Http_Https - Fatal编程技术网

C# 如何使用c将搜索字符串发布到google.com?

C# 如何使用c将搜索字符串发布到google.com?,c#,http,https,C#,Http,Https,这是我的密码 using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class Program { public static void Main () { //使用可以接收帖子的URL创建请求 WebRequest request = WebRequest.Cr

这是我的密码

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class Program
    {
        public static void Main ()
        {
//使用可以接收帖子的URL创建请求

            WebRequest request = WebRequest.Create("http://www.google.com");
//将请求的Method属性设置为POST

            request.Method = "POST";
//创建POST数据并将其转换为字节数组

           string postData = "This is a test that posts this string to a Web server.";
           byte[] byteArray = Encoding.UTF8.GetBytes (postData);
//设置WebRequest的ContentType属性

           request.ContentType = "application/x-www-form-urlencoded";
           request.ContentLength = byteArray.Length;
//设置WebRequest的ContentLength属性

           request.ContentType = "application/x-www-form-urlencoded";
           request.ContentLength = byteArray.Length;
//获取请求流

           Stream dataStream = request.GetRequestStream ();
           dataStream.Write (byteArray, 0, byteArray.Length);
//将数据写入请求流

           Stream dataStream = request.GetRequestStream ();
           dataStream.Write (byteArray, 0, byteArray.Length);
//关闭流对象

           dataStream.Close ();
//得到回应

           WebResponse response = request.GetResponse ();
//显示状态

           Console.WriteLine (((HttpWebResponse)response).StatusDescription);
//获取包含服务器返回的内容的流

           dataStream = response.GetResponseStream ();
//使用StreamReader打开流以便于访问

           StreamReader reader = new StreamReader (dataStream);
//阅读内容

           string responseFromServer = reader.ReadToEnd ();

// Display the content.

           Console.WriteLine (responseFromServer);

// Clean up the streams.

           reader.Close ();
           dataStream.Close ();
           response.Close ();
       }
    }
}
这是我得到的例外

Unhandled Exception: System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
       at System.Net.HttpWebRequest.GetResponse()
       at Examples.System.Net.Program.Main()

如果你查看Chrome开发工具,你会发现Google不使用POST进行搜索查询。而且一点也不允许


要使用WebRequest进行搜索,请使用GET To the Url=https://www.google.se/search?q=This 是一个将此字符串发布到Web服务器的测试。

我知道这个答案会有延迟:-但在花费大量时间研究如何使用HttpWebRequest(最佳解决方案)发送数据之后,对于谷歌的请求和我一般来说是使用Fiddler来检查所有的数据,以及从浏览器发送的参数。 Fiddler将帮助您验证浏览器发送到服务器的内容,即服务器使用的方法、查询字符串以及所有标头,如用户代理、接受和可能的正文字符串等。
花些时间安装Fiddler并学习使用它将为您省去许多麻烦。

这段代码有什么问题?编译错误、运行时异常、非预期行为?通过system.web dll,您可以使用Response.Redirectweb page URL。我已添加了获得的异常。请准确和详细,我对c sharp networking比较陌生。当你自己搜索时,用它来检查你的浏览器发送给谷歌的内容。我很确定他们不允许你这么容易就把他们的页面刮下来。谢谢。雷内。现在我可以问一下,我也可以在其他网站上使用这个小提琴手吗?啊,它工作起来很神奇。在使用GET-on-URL之后?谷歌会返回搜索结果的HTML代码吗?看起来他们会返回完整的页面结果。如果您希望通过API获得Json格式的搜索结果,例如,您可以使用他们的服务Google Custom Seach:谢谢。我可以使用WebRequest来填写文本字段并单击另一个网站(如Facebook.com)中的按钮吗?您可以使用WebRequest来触发按钮按下按钮时会导致的事件/帖子等,模拟浏览器行为。但这比你最初的问题要复杂得多!可以请用C代码向我解释夏皮安。我应该再问一个问题,或者你可以在这里的评论中回答我?