Is WebRequest The Right C# Tool For Interacting With Websites?

前端 未结 7 1606
醉梦人生
醉梦人生 2020-12-23 13:17

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#

相关标签:
7条回答
  • 2020-12-23 13:29

    WebClient is sometimes easier to use than WebRequest. You may want to take a look at it.

    For JSON deserialization you are going to want to look at the JavaScriptSerializer class.

    WebClient example:

    using (WebClient client = new WebClient ())
    {
        //manipulate request headers (optional)
        client.Headers.Add (HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    
        //execute request and read response as string to console
        using (StreamReader reader = new StreamReader(client.OpenRead(targetUri)))
        {
            string s = reader.ReadToEnd ();
            Console.WriteLine (s);
        }
    }
    

    Marked as wiki in case someone wants to update the code

    0 讨论(0)
  • 2020-12-23 13:29

    I have used WebRequest for interacting with websites. It is the right 'tool'

    I can't comment on the JSON aspect of your question.

    0 讨论(0)
  • 2020-12-23 13:31

    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();
    

    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:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

    0 讨论(0)
  • 2020-12-23 13:31

    When it comes to POSTing data to a web site, System.Net.HttpWebRequest (the HTTP-specific implementation of WebRequest) is a perfectly decent solution. It supports SSL, async requests and a bunch of other goodies, and is well-documented on MSDN.

    The payload can be anything: data in JSON format or whatever -- as long as you set the ContentType property to something the server expects and understands (most likely application/json, text/json or text/x-json), all will be fine.

    One potential issue when using HttpWebRequest from a system service: since it uses the IE proxy and credential information, default behavior may be a bit strange when running as the LOCALSYSTEM user (or basically any account that doesn't log on interactively on a regular basis). Setting the Proxy and Authentication properties to Nothing (or, as you C# folks prefer to call it, null, I guess) should avoid that.

    0 讨论(0)
  • 2020-12-23 13:37

    The currently highest rated answer is helpful, but it doesn't send or receive JSON.

    Here is an example that uses JSON for both sending and receiving:

    How to post json object in web service

    And here is the StackOverflow question that helped me most to solve this problem:

    Problems sending and receiving JSON between ASP.net web service and ASP.Net web client

    And here is another related question:

    json call with C#

    0 讨论(0)
  • 2020-12-23 13:50

    To convert from instance object to json formatted string and vice-versa, try out Json.NET: http://json.codeplex.com/

    I am currently using it for a project and it's easy to learn and work with and offers some flexibility in terms of serializing and custom type converters. It also supports a LINQ syntax for querying json input.

    0 讨论(0)
提交回复
热议问题