Last active
December 18, 2015 16:58
-
-
Save renatocantarino/5814984 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using cliente.Helper; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
namespace cliente | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
do | |
{ | |
try | |
{ | |
Console.WriteLine("Qual o metodo?"); | |
string metodo = Console.ReadLine(); | |
Console.WriteLine("Informe a URI:"); | |
string uri = Console.ReadLine(); | |
var _request = WebRequest.Create(uri) as HttpWebRequest; | |
_request.KeepAlive = false; | |
_request.Method = metodo.ToUpper(); | |
if (("POST,PUT").Split(',').Contains(metodo.ToUpper())) | |
Post(_request); | |
HttpWebResponse _webResponse = _request.GetResponse() as HttpWebResponse; | |
Encoding _encoding = Encoding.GetEncoding(1252); | |
var _responseStream = new StreamReader(_webResponse.GetResponseStream(), _encoding); | |
string Response = _responseStream.ReadToEnd(); | |
_responseStream.Close(); | |
_webResponse.Close(); | |
Console.WriteLine(Response); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("Continuar [S] - [N]?"); | |
} while ((Console.ReadLine().ToUpper() == "S")); | |
} | |
private static void Post(HttpWebRequest _request) | |
{ | |
var pessoa = new Pessoa { Age = "26", Name = "TROLL MASTER MAN" }; | |
var serializado = pessoa.ToJSON(); | |
byte[] buffer = Encoding.ASCII.GetBytes(serializado); | |
_request.ContentLength = buffer.Length; | |
_request.ContentType = "application/json"; | |
Stream PostData = _request.GetRequestStream(); | |
PostData.Write(buffer, 0, buffer.Length); | |
PostData.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment