Skip to main content
 Web开发网 » 站长学院 » 服务器教程

[程序设计]在Windows Mobile上实现httpwebrequest post数据到服务器上,出现远程服务器

2021年07月22日6240百度已收录

最近在Windows Mobile上实现();就出现

  远程服务器返回错误: (500) 内部服务器错误;以下是实现的源代码

   ;

   =

   (");

   req.Headers.Add("Instruction", "Tracing_MobilePost_ShouldBeSendToBrower");//服务器要求增加的自定义头

   req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0;Windows CE 6.0; Windows XP; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;.NET CLR 1.1.4322;MAXTHON 2.0)";

   req.Method = "post";

   //req.AllowAutoRedirect = true;

   req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

   req.ContentType = "application/x-";

   // req.ContentType = "application/xml";

   req.KeepAlive=true;

   req.SendChunked = true;

   long date = (System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks) / 10000

   simno = "000000000000000";

   StringBuilder sb = new StringBuilder();

   sb.Append("simno=");

   sb.Append(simno);

   sb.Append("&");

   // sb.Append(";");

   // sb.Append("\r\n");

   sb.Append("lat=");

   sb.Append(9.65544);

   // sb.Append("\r\n");

   sb.Append("&");

   // sb.Append(";");

   sb.Append("lon=");

   sb.Append(145.10829);

   //sb.Append("\r\n"); ;

   sb.Append("&");

   //sb.Append(";");

   sb.Append("heit=");

   sb.Append(32.1);

   // sb.Append("\r\n");

   sb.Append("&");

   // sb.Append(";");

   sb.Append("date=");

   sb.Append(date);

   // sb.Append("\r\n");

   sb.Append("&");

   // sb.Append(";");

   sb.Append("text=");

   sb.Append(m_txtmessage.Text);

   byte[] byte1 = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sb.ToString());

   req.ContentLength = byte1.Length;

   System.IO.Stream newStream = req.GetRequestStream();

   newStream.Write(byte1, 0, byte1.Length);

   newStream.Close();

   // try

   resp = (错误

   // catch(WebException ex)

   // resp = (;

   if (resp.StatusCode == )

   using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))

   txtListen.Text = string.Format("接收的数据的内容长度:{0}\r\n",

   resp.ContentLength,

   resp.ContentType,

   resp.Method,

   resp.ResponseUri,

   resp.StatusCode,

   resp.StatusDescription,

   sr.ReadToEnd());

   // txtListen.Text = resp.GetResponseHeader("Instruction") + sr.ReadToEnd(); ;

   // txtListen.Text = resp.Headers+resp.GetResponseHeader("Instruction") +sr.ReadToEnd();

   sr.Close();

   resp.Close();

  如果将req.ContentType改成 "application/xml";则不会出现500错误,但返回数据的长度为0,即Content-Length:0;服务器那边是规定如果发送数据正确接收则有类似这样的Instruction_Tracing_ServerResponse=Normal回应。但是目前却什么也接收不到。

评论列表暂无评论
发表评论
微信