using CsharpHttpHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PostImage
{
public partial class Form1 : Form
{
bool mAutoPost = false;
bool mIsExit = false;
int mLoopNum = 0;

public Form1()
{
InitializeComponent();
(new Thread(threadAutoPost)).Start();
}

public void threadAutoPost()
{
int num = 1;
while(mIsExit == false)
{
Thread.Sleep(1000);
if (mAutoPost)
{
for(int i = 0; i <= 100; ++i)
{
doAutoPost();
mLoopNum += 1;
}
}
}
}

public void doAutoPost()
{
string Question = "test2020" + mLoopNum;
string imgDir = @"E:\work\tmp\AI\demo\pic3";
string[] files = Directory.GetFiles(imgDir);
foreach (string file in files)
{
string imgPath = Path.Combine(imgDir, file);
autoPost(Question, imgPath);
}
}


public void autoPost(string Question, string imagePath)
{
string[] arrFileName = Path.GetFileName(imagePath).Split('_');
if (arrFileName.Length != 2) return;

string RequestId = "RI123456";
string UserName = "test";
string Password = "test";
//string Question = "test2020";
//string Answer = "1234";
string Answer = arrFileName[0];
string boundary = "------E6DA7D5FD362467EAA74F8228F313B34";

HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem();
item.URL = "http://127.0.0.1/train";
item.Method = "Post";
item.ContentType = "multipart/form-data; boundary=----E6DA7D5FD362467EAA74F8228F313B34";
item.Referer = "http://127.0.0.1/train";
item.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";

byte[] postData = null;
StringBuilder strBuilder = new StringBuilder();

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"RequestId\"").Append("\r\n\r\n");
strBuilder.Append(RequestId).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"UserName\";").Append("\r\n\r\n");
strBuilder.Append(UserName).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Password\";").Append("\r\n\r\n");
strBuilder.Append(Password).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Question\";").Append("\r\n\r\n");
strBuilder.Append(Question).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Answer\";").Append("\r\n\r\n");
strBuilder.Append(Answer).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Image\";").Append("\r\n\r\n");

Encoding webEncoding = Encoding.UTF8;
postData = webEncoding.GetBytes(strBuilder.ToString());
byte[] PicBytes = ImageToBytesFromFilePath(imagePath);
postData = ComposeArrays(postData, PicBytes);

string tail = "\r\n" + boundary + "--";
postData = ComposeArrays(postData, webEncoding.GetBytes(tail));

item.PostDataType = CsharpHttpHelper.Enum.PostDataType.Byte;
item.PostdataByte = postData;
item.Accept = "*/*";
item.Header.Add("Accept-Language", "zh-cn");

item.PostEncoding = webEncoding;
HttpResult result = http.GetHtml(item);
//MessageBox.Show(result.Html);
string strLog = string.Format("{0} {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), result.Html);
this.BeginInvoke((ThreadStart)delegate()
{
labelIMsg.Text = strLog;
});
}

private void btnPostImage_Click(object sender, EventArgs e)
{
string RequestId = "RI123456";
string UserName = "test";
string Password = "test";
string Question = "test2020";
string Answer = "1234";
string boundary = "------E6DA7D5FD362467EAA74F8228F313B34";

HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem();
item.URL = "http://127.0.0.1/train";
item.Method = "Post";
item.ContentType = "multipart/form-data; boundary=----E6DA7D5FD362467EAA74F8228F313B34";
item.Referer = "http://127.0.0.1/train";
item.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";

byte[] postData = null;
StringBuilder strBuilder = new StringBuilder();

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"RequestId\"").Append("\r\n\r\n");
strBuilder.Append(RequestId).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"UserName\";").Append("\r\n\r\n");
strBuilder.Append(UserName).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Password\";").Append("\r\n\r\n");
strBuilder.Append(Password).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Question\";").Append("\r\n\r\n");
strBuilder.Append(Question).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Answer\";").Append("\r\n\r\n");
strBuilder.Append(Answer).Append("\r\n");

strBuilder.Append(boundary).Append("\r\n");
strBuilder.Append("Content-Disposition: form-data; name=\"Image\";").Append("\r\n\r\n");

Encoding webEncoding = Encoding.UTF8;
postData = webEncoding.GetBytes(strBuilder.ToString());
byte[] PicBytes = ImageToBytesFromFilePath(@"E:\work\img.png");
postData = ComposeArrays(postData, PicBytes);

string tail = "\r\n" + boundary + "--";
postData = ComposeArrays(postData, webEncoding.GetBytes(tail));

item.PostDataType = CsharpHttpHelper.Enum.PostDataType.Byte;
item.PostdataByte = postData;
item.Accept = "*/*";
item.Header.Add("Accept-Language", "zh-cn");

item.PostEncoding = webEncoding;
HttpResult result = http.GetHtml(item);
//MessageBox.Show(result.Html);
string strLog = string.Format("{0} {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), result.Html);
this.BeginInvoke((ThreadStart)delegate()
{
labelIMsg.Text = strLog;
});
}

private byte[] ImageToBytesFromFilePath(string FilePath)
{
FileInfo fInfo = new FileInfo(FilePath);
long fileLen = fInfo.Length;
FileStream fs = new FileStream(FilePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] data = br.ReadBytes((int)fileLen);
br.Close();
fs.Close();
return data;
}

public static byte[] ComposeArrays(byte[] Array1, byte[] Array2)
{
byte[] Temp = new byte[Array1.Length + Array2.Length];
Array1.CopyTo(Temp, 0);
Array2.CopyTo(Temp, Array1.Length);
return Temp;
}

private void chbAutoPost_CheckedChanged(object sender, EventArgs e)
{
mAutoPost = chbAutoPost.Checked;
}



}
}