📄 httpwebrequesthelper.cs
字号:
namespace Imps.Utils
{
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
public class HttpWebRequestHelper
{
private System.Text.Encoding encoding = System.Text.Encoding.UTF8;
public byte[] CreateFieldData(string fieldName, string fieldValue)
{
string s = string.Format(this.Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", fieldName, fieldValue);
return this.encoding.GetBytes(s);
}
public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes)
{
string s = "\r\n";
string text3 = string.Format(this.Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", fieldName, filename, contentType);
byte[] bytes = this.encoding.GetBytes(text3);
byte[] buffer2 = this.encoding.GetBytes(s);
byte[] array = new byte[(bytes.Length + fileBytes.Length) + buffer2.Length];
bytes.CopyTo(array, 0);
fileBytes.CopyTo(array, bytes.Length);
buffer2.CopyTo(array, (int) (bytes.Length + fileBytes.Length));
return array;
}
public System.Text.Encoding GetEncoding(HttpWebResponse response)
{
string name = response.ContentEncoding;
System.Text.Encoding encoding = System.Text.Encoding.Default;
if (name == "")
{
string contentType = response.ContentType;
if (contentType.ToLower().IndexOf("charset") != -1)
{
name = contentType.Substring(contentType.ToLower().IndexOf("charset=") + "charset=".Length);
}
}
if (name == "")
{
return encoding;
}
try
{
return System.Text.Encoding.GetEncoding(name);
}
catch
{
}
}
internal static char IntToHex(int n)
{
if (n <= 9)
{
return (char) (n + 0x30);
}
return (char) ((n - 10) + 0x61);
}
internal static bool IsSafe(char ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return true;
}
switch (ch)
{
case '\'':
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return true;
}
return false;
}
public byte[] JoinBytes(ArrayList byteArrays)
{
int num = 0;
int index = 0;
string s = this.Boundary + "--\r\n";
byte[] bytes = this.encoding.GetBytes(s);
byteArrays.Add(bytes);
foreach (byte[] buffer2 in byteArrays)
{
num += buffer2.Length;
}
byte[] array = new byte[num];
foreach (byte[] buffer4 in byteArrays)
{
buffer4.CopyTo(array, index);
index += buffer4.Length;
}
return array;
}
public string TextContent(HttpWebResponse response)
{
string text2;
string text = "";
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, this.GetEncoding(response));
while ((text2 = reader.ReadLine()) != null)
{
text = text + text2 + "\r\n";
}
responseStream.Close();
return text;
}
public static string UrlEncode(string str)
{
if (str == null)
{
return null;
}
return UrlEncode(str, System.Text.Encoding.UTF8);
}
public static string UrlEncode(string str, System.Text.Encoding e)
{
if (str == null)
{
return null;
}
return System.Text.Encoding.ASCII.GetString(UrlEncodeToBytes(str, e));
}
private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
{
int num = 0;
int num2 = 0;
for (int i = 0; i < count; i++)
{
char ch = (char) bytes[offset + i];
if (ch == ' ')
{
num++;
}
else if (!IsSafe(ch))
{
num2++;
}
}
if ((!alwaysCreateReturnValue && (num == 0)) && (num2 == 0))
{
return bytes;
}
byte[] buffer = new byte[count + (num2 * 2)];
int num4 = 0;
for (int j = 0; j < count; j++)
{
byte num6 = bytes[offset + j];
char ch2 = (char) num6;
if (IsSafe(ch2))
{
buffer[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer[num4++] = 0x2b;
}
else
{
buffer[num4++] = 0x25;
buffer[num4++] = (byte) IntToHex((num6 >> 4) & 15);
buffer[num4++] = (byte) IntToHex(num6 & 15);
}
}
return buffer;
}
public static byte[] UrlEncodeToBytes(byte[] bytes)
{
if (bytes == null)
{
return null;
}
return UrlEncodeToBytes(bytes, 0, bytes.Length);
}
public static byte[] UrlEncodeToBytes(string str, System.Text.Encoding e)
{
if (str == null)
{
return null;
}
byte[] bytes = e.GetBytes(str);
return UrlEncodeBytesToBytesInternal(bytes, 0, bytes.Length, false);
}
public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count)
{
if ((bytes == null) && (count == 0))
{
return null;
}
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
if ((offset < 0) || (offset > bytes.Length))
{
throw new ArgumentOutOfRangeException("offset");
}
if ((count < 0) || ((offset + count) > bytes.Length))
{
throw new ArgumentOutOfRangeException("count");
}
return UrlEncodeBytesToBytesInternal(bytes, offset, count, true);
}
public string Boundary
{
get
{
string[] textArray2 = this.ContentType.Split(new char[] { ';' });
if (textArray2[0].Trim().ToLower() == "multipart/form-data")
{
string[] textArray = textArray2[1].Split(new char[] { '=' });
return ("--" + textArray[1]);
}
return null;
}
}
public string ContentType
{
get
{
return "multipart/form-data; boundary=---------------------------7d5b915500cee";
}
}
public System.Text.Encoding Encoding
{
get
{
return this.encoding;
}
set
{
this.encoding = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -