Network programming of http Protocol of C #
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
{
/ / /
/ C # tool class for interfacing with http server
/ / /
Class HttpWebTool
{
/ / /
/ / used to cache SESSIONID or JSESSIONID transferred from the server to the client
/ / /
Private Cookie sessionidCookie = null
/ / /
/ / get data from HttpWebServer (using "post" mode)
/ / /
/ / request URL
/ / request parameter collection. Null value is passed when no parameter is needed.
/ / request cookie collection. Pass a null value when no cookie is required.
/ / return the request result string, which means that the request failed if returned as null
Public String getDatafromHttpWebServer (String url, Hashtable data,CookieCollection cookies)
{
String result = null
If (string.IsNullOrEmpty (url))
{
Return null;// input parameter exception
}
Byte [] data_stream = null;// the contents 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 the 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 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 created null)
{
CookieContainer.Add (cookies); / / add the cookie collection passed in by the caller
}
Req.CookieContainer = cookieContainer
If (data_stream! = null & & data_stream.Length > 0)
{
/ / request the length of the data stream
Req.ContentLength = data_stream.Length
Using (Stream requestStream = req.GetRequestStream ()) {
/ / write request entity stream
RequestStream.Write (data_stream, 0, data_stream.Length)
}
}
/ / receive the 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 down")
}
Return result
}
/ / /
/ / get the message data flow for the parameter data, ending with "\ r\ n"
/ / /
/ / request parameter collection. Null value is passed when no parameter is needed.
/ / message delimiter
/ / return the data flow of the parameter data. If it is empty, it means the acquisition failed.
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 uploaded string data into a data stream
}
}
Return parameterBytes
}
/ / /
/ / get the header character stream of the uploaded file, ending with "\ r\ n\ r\ n"
/ / /
/ / upload file "control name, location where the uploaded file is saved (including" file name "." extension ")"
/ / message delimiter
/ / return the character stream of the header part of the uploaded file. The return will fail for the null representative.
Private byte [] getUploadFileDeclareBytes (DictionaryEntry de, String boundary)
{
Byte [] uploadFileDeclareBytes = null
/ / the 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 uploaded string data into a data stream
}
Return uploadFileDeclareBytes
}
}
}
The ownership of the content belongs to Yuekang Sports (professional research physique tester, student physique tester)