??????ò????2????????δ??????C#?е?Send WebRequest???????????.NET???????????????????????????????????????????????????API?????Send WebRequest??????????API?????????????????Selenium????????????????????????????????????????????г?????HTTP request????????????????????????????
????1???????Address???Webrequest???????????????趨????????????Method??Get????Post????
????2???????Data???HttpRequest????????д??
????XmlResponse = null;
????ResponseCode = HttpStatusCode.Unused;
????ServerResponse = String.Empty;
???????????????

 

//Format the data
string output = data.Aggregate(String.Empty?? (current?? keyValuePair) => current + ("&" + keyValuePair.Key + "=" + HttpUtility.UrlEncode(keyValuePair.Value)));
var encoding = new UTF8Encoding();
//????PostData??????output???д????????byte[] d?????????д??WebRequest??
var Postdata = Encoding.ASCII.GetBytes(output);
byte[] d = encoding.GetBytes(output.Substring(1?? output.Length - 1));
var address = TargetUrl;
//GET????????????
if (method.ToUpperInvariant() == "GET")
address=address+output;
//POST????????????
WebRequest request = WebRequest.Create(address) as HttpWebRequest;
//Add post process
if (request == null)
throw new Exception("WebRequest object is null.");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = d.Length;
ServerResponse = String.Empty;
//???????????????
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(d?? 0?? d.Length);
dataStream.Close();
using (var response = (HttpWebResponse)request.GetResponse())
{
ResponseCode = response.StatusCode;
using (Stream myStream = response.GetResponseStream())
{
if (myStream != null)
{
var readStream = new StreamReader(myStream?? Encoding.UTF8);
ServerResponse = readStream.ReadToEnd();
}
}
response.Close();
}
}
if (ResponseCode == HttpStatusCode.OK)
{
if (loadXml)
LoadXml();
return true;
}
return false;