WebRequest的是正确的C#工具交互使用的网站? [英] Is WebRequest The Right C# Tool For Interacting With Websites?

查看:118
本文介绍了WebRequest的是正确的C#工具交互使用的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中的小工具,这将需要使用POST和JSON格式的网站发送和接收数据/。我从来没有在C#中做过这样的事(或者任何其他语言),所以我在努力寻找让我开始了一些有用的信息。

I'm writing a small tool in C# which will need to send and receive data to/from a website using POST and json formatting. I've never done anything like this before in C# (or any language really) so I'm struggling to find some useful information to get me started.

我发现在C#WebRequest类的一些信息(尤其是从这里)但在我开始深入到它,我想知道,如果这是正确的工具的工作。

I've found some information on the WebRequest class in C# (specifically from here) but before I start diving into it, I wondered if this was the right tool for the job.

我发现很多的工具将数据转换成JSON格式,但没有多少人,所以在情况下,我最终下了穷途末路的任何信息会在这里非常有帮助。

I've found plenty of tools to convert data into the json format but not much else, so any information would be really helpful here in case I end up down a dead end.

推荐答案

WebRequest的,更具体的HttpWebRequest类是一个很好的起点,你想达到什么。要创建请求将使用类向WebRequest.Create投下创建的请求到HttpWebRequest的实际使用它。然后,您将创建您的文章数据,并像发送到流:

WebRequest and more specifically the HttpWebRequest class is a good starting point for what you want to achieve. To create the request you will use the WebRequest.Create and cast the created request to an HttpWebRequest to actually use it. You will then create your post data and send it to the stream like:

HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "var=value1&var2=value2";
req.ContentLength = postData.Length;

StreamWriter stOut = new
StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(postData);
stOut.Close();

同样,你可以通过使用GetResponse方法,让您阅读所产生的响应流,做任何你需要做的读回响应。你可以找到在类的更多信息:

Similarly you can read the response back by using the GetResponse method which will allow you to read the resultant response stream and do whatever else you need to do. You can find more info on the class at:

<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

这篇关于WebRequest的是正确的C#工具交互使用的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆