The content is very good and can be used directly. The ownership of the content belongs to: http://www.xinduofen.com/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace www.xinduofen.cn
{
/// <summary>
/// A tool class for C# to connect with the http server
/// </summary>
class HttpWebTool
{
/// <summary>
/// Used to cache the SESSIONID or JSESSIONID transmitted from the server to the client
/// </summary>
private Cookie sessionidCookie = null;
/// <summary>
/// Get data from HttpWebServer (using "post" method)
/// </summary>
/// <param name="url">Request URL</param>
/// <param name="data">Request parameter set, pass in null value when no parameter is required</param>
/// <param name="cookies">Request cookie collection, pass in null value when no cookie is needed</param>
/// <returns>returns the request result string, if null is returned, the request fails</returns>
public String getDatafromHttpWebServer(String url, Hashtable data,CookieCollection cookies)
{
String result = null;
if (string.IsNullOrEmpty(url))
{
return null;//Incoming parameter exception
}
byte[] data_stream = null;//The content of the data stream to be transmitted to the server
if (data != null && data.Count > 0)
{
string transportData = "";//The string content to be transmitted to the server
foreach (DictionaryEntry de in data)
{
transportData = transportData + de.Key.ToString() + "=" + de.Value.ToString() + "&";//Demodulate key-value pair data
}
transportData = transportData.TrimEnd('&');//Remove the & at the end of the string
if (!string.IsNullOrEmpty(transportData))
{
data_stream = Encoding.UTF8.GetBytes(transportData);//Package the uploaded string data into a data stream
}
}
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
// request method
req.Method = "POST";
//Declare that the client only receives content of type txt
req.Accept = "text/plain";
// Pass parameters to the server in the form of key-value pairs
req.ContentType = "application/x-www-form-urlencoded";
//Set the cookie box (the cookie requested by the client and the cookie returned by the server are placed in this box)
CookieContainer cookieContainer = new CookieContainer();
if (sessionidCookie != null && !string.IsNullOrEmpty(sessionidCookie.Domain))
{
cookieContainer.Add(sessionidCookie);
}
if (cookies!=null)
{
cookieContainer.Add(cookies);//Add the set of cookies passed in by the caller
}
req.CookieContainer = cookieContainer;
if (data_stream != null && data_stream.Length > 0)
{
// The length of the request data stream
req.ContentLength = data_stream.Length;
using (Stream requestStream = req.GetRequestStream()) {
//Write the request entity stream
requestStream.Write(data_stream, 0, data_stream.Length);
}
}
// receive return value
using(HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse()){
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd().Trim();
}
if (myResponse.Cookies["SESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["SESSIONID"];
}
else
{
if (myResponse.Cookies["JSESSIONID"] != null)
{
sessionidCookie = myResponse.Cookies["JSESSIONID"];
}
}
}
}catch(Exception){
Console.WriteLine("Please check whether the incoming parameters are correct or whether the server is closed");
}
return result;
}
/// <summary>
/// Get the message data stream of parameter data, ending with "\r\n"
/// </summary>
/// <param name="data">Request parameter set, pass in null value when no parameter is required</param>
/// <param name="boundary">Message delimiter</param>
/// <returns>Returns the data stream of the parameter data, if the return is empty, it means the acquisition fails</returns>
private byte[] getParameterBytes(Hashtable data, String boundary)
{
byte[] parameterBytes = null;
//if there are request parameters
if (data != null && data.Count > 0)
{
string parameterStr = "";
foreach (DictionaryEntry de in data)
{
parameterStr += "--" + boundary;
parameterStr += "\r\n" + "Content-Disposition: form-data;name=\"" + de.Key.ToString() + "\"";
parameterStr += "\r\n" + "Content-Type: text/plain; charset=UTF-8";
parameterStr += "\r\n\r\n" + de.Value.ToString();
parameterStr += "\r\n";
}
if (!string.IsNullOrEmpty(parameterStr))
{
parameterBytes = Encoding.UTF8.GetBytes(parameterStr);//Package the uploaded string data into a data stream
}
}
return parameterBytes;
}
/// <summary>
/// Get the character stream of the message header part of the uploaded file, ending with "\r\n\r\n"
/// </summary>
/// <param name="de">Upload file "Control name, save location of uploaded file (including "file name"."Extension")"</param>
/// <param name="boundary">Message delimiter</param>
/// <returns>Returns the character stream of the message header part of the uploaded file, the return will be null, which means the acquisition fails</returns>
private byte[] getUploadFileDeclareBytes(DictionaryEntry de, String boundary)
{
byte[] uploadFileDeclareBytes = null;
//The message header description part of the uploaded file
string uploadFileDeclareStr = "";
uploadFileDeclareStr += "--" + boundary;
uploadFileDeclareStr += "\r\n" + "Content-Disposition: form-data;name=\"" + de.Key.ToString() + "\"; filename=\"" + de.Value.ToString() + "\"";
uploadFileDeclareStr += "\r\n" + "Content-Type: application/octet-stream";
uploadFileDeclareStr += "\r\n\r\n";
if (!string.IsNullOrEmpty(uploadFileDeclareStr))
{
uploadFileDeclareBytes = Encoding.UTF8.GetBytes(uploadFileDeclareStr);//Package the uploaded string data into a data stream
}
return uploadFileDeclareBytes;
}
}
}
Content ownership belongs to: Yuekang Sports (professional research on physical fitness testing equipment , physical fitness testing equipment )