tags:

views:

412

answers:

3

Hi, I want to code a auto bot for a online game (tribalwars.net) , I taking Programming A Lessons in CSharp (In School), but Network Programming coming in B or C

Anyway I wonder if it is possible to make http post though C#, Can give a example

A: 

Here's a good example. You want to use the WebRequest class in C#, which will make this easy.

Dave Markle
+2  A: 

Request

HttpWebRequest request= (HttpWebRequest)WebRequest.Create(url);
request.ContentType="application/x-www-form-urlencoded";
request.Method = "POST";
request.KeepAlive = true;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(BytePost,0,BytePost.Length);
    requestStream.Close();
}

Response

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using(StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    responseString = sr.ReadToEnd();
}
cgreeno
You should encase the using in a try-catch block so that you can capture data from 400 or 500 errors.
Spencer Ruport
+6  A: 

Trivial with System.Net.WebClient:

using(WebClient client = new WebClient()) {
    string responseString = client.UploadString(address, requestString);
}

There is also:

  • UploadData - binary (byte[])
  • UploadFile - from a file
  • UploadValues - name/value pairs (like a form)
Marc Gravell