Created
March 6, 2015 12:54
-
-
Save gabfr/18894198050020f1e1fe to your computer and use it in GitHub Desktop.
Test with multipartformdata
This file contains 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 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