Skip to content

Instantly share code, notes, and snippets.

@gabfr
Created March 6, 2015 12:54
Show Gist options
  • Save gabfr/18894198050020f1e1fe to your computer and use it in GitHub Desktop.
Save gabfr/18894198050020f1e1fe to your computer and use it in GitHub Desktop.
Test with multipartformdata
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace DownloadTest
{
public class Class1
{
public void UsingDownloadExample()
{
using (var downFile = Download("http://static.tibia.com/download/tibia1076.tgz"))
{
}
}
public Stream Download(string link)
{
try
{
using (HttpWebResponse response = CreateRequest(link))
{
if (response.StatusCode == HttpStatusCode.OK)
{
return response.GetResponseStream();
}
else
{
// Tratar outros status codes
return null;
}
}
}
catch (WebException ex)
{
Console.Error.Write(ex.ToString());
return null;
}
}
public HttpWebResponse CreateRequest(string link, string postArgs = null)
{
HttpWebRequest request;
try
{
request = (HttpWebRequest) WebRequest.Create(link);
request.ContentType = "";
request.Method = "GET";// GET
request.Timeout = 30*1000;// timeout in milliseconds
//request.Headers.Add("x-teste", "lol");
if (request.Method.ToLower() == "post")
{
//var multipart = new MultipartFormDataContent();
using (Stream reqStream = request.GetRequestStream())
{
byte[] byteArray = Encoding.UTF8.GetBytes(postArgs);
reqStream.Write(byteArray, 0, byteArray.Length);
reqStream.Close();
}
}
return (HttpWebResponse) request.GetResponse();
}
catch (WebException ex)
{
throw ex;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment