PRivate string postString =
System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();
string body = string.Format(@"<?x
ML version=""1.0"" encoding=""UTF-8""?>
<Body>
<ValidId>{0}</ValidId>
<OrderId>{1}</OrderId>
<Count>{2}</Count>
<ValidTime>{3}</ValidTime>
<Remark/>
</Body>", consumption.Id, consumption.Order.AgentOrderId, consumption.Count, consumption.CreateTime.DateTimeToDa
testring("
yyyy-MM-dd HH:mm:ss"));
&nbs
p; string request = BuildRequest(body);
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(postString);
hwr.Method = "POST";
hwr.Headers.Add("X-Auth
-token", HttpUtil
ITy.UrlEncode("o
PEnstack"));
hwr.ContentType = "application/json";
//hwr.Accept = "application/xml";
hwr.AllowAuto
redirect = true;
byte[] dates = Encoding.UTF8.GetBytes(request);
int count = dates.Length;
//Stre
am newStream = hwr.GetRequestStream();
MemoryStream newStream = new MemoryStream();
try
{
LOG.Add("开始请求");
newStream.Write(dates, 0, dates.Length);
hwr.ContentLength = newStream.Length;
Stream requestStream = hwr.GetRequestStream();
newStream.Position = 0L;
newStream.CopyTo(requestStream);
newStream.Close();
requestStream.Close();
在这个地方值得我注意的是刚开始的时候我最早的MemoryStream用的是Stream。但是Stream数据流会莫名的报错。Stream数据流不能进行length查找操作
HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
Stream stream = null;
stream= hwResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
string file = reader.ReadToEnd();
UTF8Encoding UTF = new UTF8Encoding();
Byte[] Bytes = UTF.GetBytes(file);
file = UTF.GetString(Bytes);
string strBody = TCodeServiceCrypt.Decrypt3DES
FromBase64(GetElementValue(doc.Element("Response").Element("Body")), UserFunc.SecretKey);
XDocument xBody = XDocument.Parse(strBody);
string userId = GetElementValue(xBody.Element("Body").Element("Us
eid"));