[C#] 纯文本查看 复制代码 //long length = 0;
string debug = "";
if (boundary == "----------------------------")
boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
WebResponse webResponse2 = null;
StreamReader reader2 = null;
HttpWebResponse response = null;
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
boundary;
httpWebRequest2.CookieContainer = cookie;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpWebRequest2.Accept = "*/*";//设置标头
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Referer = "XXXXX";
httpWebRequest2.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
httpWebRequest2.Headers.Add("Accept-Language", "zh-CN");//设置标头
httpWebRequest2.Headers.Add("Cache-Control", "no-cache");//设置标头
try
{
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" +
boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
string formitem = string.Format(formdataTemplate, key, nvc[key]); debug += formitem;
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; fname=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
for (int i = 0; i < files.Length; i++)
{
//string header = string.Format(headerTemplate, "file" + i, files[i]);
string header = string.Format(headerTemplate, "Filedata", System.IO.Path.GetFileName(files[i]));
debug += header;
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
debug += System.Text.Encoding.Default.GetString(buffer);
memStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
//name="Upload"
string Uploads = "--" + boundary +
"\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\r\nSubmit Query\r\n";
debug = debug + Uploads;
memStream.Write(System.Text.Encoding.UTF8.GetBytes(Uploads), 0, Uploads.Length);
// string temstr = System.Text.Encoding.Default.GetString(boundarybytes); // End boundary
debug = debug + "--cvaxveuvmflnnomwapfghqjhlpfcrkyx--";
byte[] tem = System.Text.Encoding.UTF8.GetBytes("--cvaxveuvmflnnomwapfghqjhlpfcrkyx--");
memStream.Write(tem, 0, tem.Length);
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream(); //PostingStream
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
//StreamReader sr = new StreamReader(requestStream, Encoding.UTF8);
//StringBuilder strBuilder = new StringBuilder();
//while (-1 != sr.Peek())
//{
// strBuilder.Append(sr.ReadLine() + "\r\n ");
//}
//string resu=strBuilder.ToString();
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
webResponse2 = httpWebRequest2.GetResponse();
response = (HttpWebResponse)webResponse2;
//StreamReader SR = new StreamReader(response.GetResponseStream());
Stream stream2 = webResponse2.GetResponseStream();
reader2 = new StreamReader(stream2);
debug += "\r\n提交后Cookie:" + cookie.GetCookieHeader(new Uri(url));//拼接提交后cookie,用于分析
debug = debug + "\r\n\r\n=================================================\r\n\r\n本次请求:" + url + " 响应结果:" + response.StatusCode + "\r\n\r\n" + debug + "\r\n" + httpWebRequest2.CookieContainer.GetCookieHeader(new Uri(url)) + "\r\nrequest:\r\n" + httpWebRequest2.Headers.ToString() + "\r\nresponse:\r\n" + response.Headers.ToString() + "\r\n\r\n=================================================\r\n\r\n";
System.IO.File.WriteAllText("aa.txt", debug);
return reader2.ReadToEnd();
}
catch
{
return "";
}
finally
{
if (webResponse2 != null)
{
webResponse2.Close();
webResponse2 = null;
}
if (reader2 != null)
reader2.Close();
httpWebRequest2 = null;
} |