In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how C # uses the http protocol to communicate with the server". The explanation in the article is simple and clear, and is easy to learn and understand. Please follow the editor's train of thought to study and learn "how C # uses the http protocol to communicate with the server".
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
}
}
}
Thank you for your reading, the above is the content of "how C # uses http protocol to communicate with server". After the study of this article, I believe you have a deeper understanding of how C # uses http protocol to communicate with server, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.