65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Work._29Tools.BLL
|
|
{
|
|
public class HttpResponseBLL
|
|
{
|
|
|
|
public static string ReadResponse(HttpWebResponse response, Encoding encoding = null)
|
|
{
|
|
using (Stream responseStream = response.GetResponseStream())
|
|
{
|
|
Stream streamToRead = responseStream;
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
{
|
|
streamToRead = new GZipStream(streamToRead, CompressionMode.Decompress);
|
|
}
|
|
else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
{
|
|
streamToRead = new DeflateStream(streamToRead, CompressionMode.Decompress);
|
|
}
|
|
if (encoding == null) encoding = Encoding.UTF8;
|
|
using (StreamReader streamReader = new StreamReader(streamToRead, encoding))
|
|
{
|
|
return streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
public static void WriteMultipartBodyToRequest(HttpWebRequest request, string body)
|
|
{
|
|
string[] multiparts = Regex.Split(body, @"<!>");
|
|
byte[] bytes;
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
foreach (string part in multiparts)
|
|
{
|
|
if (File.Exists(part))
|
|
{
|
|
bytes = File.ReadAllBytes(part);
|
|
}
|
|
else
|
|
{
|
|
bytes = System.Text.Encoding.UTF8.GetBytes(part.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n"));
|
|
}
|
|
|
|
ms.Write(bytes, 0, bytes.Length);
|
|
}
|
|
|
|
request.ContentLength = ms.Length;
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
ms.WriteTo(stream);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|