ASP.NET以Post方式向网页发送数据


项目中用户提出了新要求,把本来在项目内平台内发送的信息同时发送到手机上,好在他们已经有了短信的发送平台,只要调用其接口发送就可以了。

  短信发送接口是用jsp实现的一个网页,调用方式是以Post方式向该网页发送数据。

  在网络上查找资料,几乎都是同一个结果:

 System.Net.WebRequest req = System.Net.WebRequest.Create(URI);

  req.Proxy = new System.Net.WebProxy(ProxyString, true);

  再根据用户给定的接口说明和java例子修改,结果总是返回的结果是乱码,再到网上查,说是编码方式的问题,那没有办法了,只有多方尝试了。经过近一天的不断试验,终于成功了。我的正确的代码如下:

  protected string SendMsg(string xmlMsg)

  {

  string urlPage = "http://www.handtimes.com/interface/forSCMIS.jsp";

  Stream outstream = null;

  Stream instream = null;

  StreamReader sr = null;

  HttpWebResponse response = null;

  HttpWebRequest request = null;

  // 要注意的这是这个编码方式,我尝试了很长的时间,还有内容的Xml内容的编码方式

  Encoding encoding = Encoding.GetEncoding("GBK");

  byte[] data = encoding.GetBytes(xmlMsg);

  // 准备请求...

  // 设置参数

  request = WebRequest.Create(urlPage) as HttpWebRequest;

  request.Method = "POST";

  // 这个地方的内容类型是接口文档上要求的,必须是这样的

  request.ContentType = "text/plain";

  request.ContentLength = data.Length;

  outstream = request.GetRequestStream();

  outstream.Write(data, 0, data.Length);

  outstream.Flush();

  outstream.Close();

  //发送请求并获取相应回应数据

  response = request.GetResponse() as HttpWebResponse;

  //直到request.GetResponse()程序才开始向目标网页发送Post请求

  instream = response.GetResponseStream();

  sr = new StreamReader(instream, encoding);

  //返回结果网页(html)代码

  string content = sr.ReadToEnd();

  return content;

  }

  要说明的是,发送时地数据的编码和发送的内容(xml)的编码都是使用的GBK编码时成功了,

  因为用户给我的帐号不能发送到我自己的手机上,所以我不敢进行太多的尝试,成功后就没有再继续尝试,不知道影响返回的内容是乱码的是哪一个编码,还是两个都影响。

 req.ContentType = "application/x-www-form-urlencoded";

  req.Method = "POST";

  byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);

  req.ContentLength = bytes.Length;

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

  os.Write (bytes, 0, bytes.Length);

  os.Close ();

  System.Net.WebResponse resp = req.GetResponse();

  if (resp== null) return null;

  System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

  return sr.ReadToEnd().Trim();

项目中用户提出了新要求,把本来在项目内平台内发送的信息同时发送到手机上,好在他们已经有了短信的发送平台,只要调用其接口发送就可以了。

  短信发送接口是用jsp实现的一个网页,调用方式是以Post方式向该网页发送数据。

  在网络上查找资料,几乎都是同一个结果:

 System.Net.WebRequest req = System.Net.WebRequest.Create(URI);

  req.Proxy = new System.Net.WebProxy(ProxyString, true);

  再根据用户给定的接口说明和java例子修改,结果总是返回的结果是乱码,再到网上查,说是编码方式的问题,那没有办法了,只有多方尝试了。经过近一天的不断试验,终于成功了。我的正确的代码如下:

  protected string SendMsg(string xmlMsg)

  {

  string urlPage = "http://www.handtimes.com/interface/forSCMIS.jsp";

  Stream outstream = null;

  Stream instream = null;

  StreamReader sr = null;

  HttpWebResponse response = null;

  HttpWebRequest request = null;

  // 要注意的这是这个编码方式,我尝试了很长的时间,还有内容的Xml内容的编码方式

  Encoding encoding = Encoding.GetEncoding("GBK");

  byte[] data = encoding.GetBytes(xmlMsg);

  // 准备请求...

  // 设置参数

  request = WebRequest.Create(urlPage) as HttpWebRequest;

  request.Method = "POST";

  // 这个地方的内容类型是接口文档上要求的,必须是这样的

  request.ContentType = "text/plain";

  request.ContentLength = data.Length;

  outstream = request.GetRequestStream();

  outstream.Write(data, 0, data.Length);

  outstream.Flush();

  outstream.Close();

  //发送请求并获取相应回应数据

  response = request.GetResponse() as HttpWebResponse;

  //直到request.GetResponse()程序才开始向目标网页发送Post请求

  instream = response.GetResponseStream();

  sr = new StreamReader(instream, encoding);

  //返回结果网页(html)代码

  string content = sr.ReadToEnd();

  return content;

  }

  要说明的是,发送时地数据的编码和发送的内容(xml)的编码都是使用的GBK编码时成功了,

  因为用户给我的帐号不能发送到我自己的手机上,所以我不敢进行太多的尝试,成功后就没有再继续尝试,不知道影响返回的内容是乱码的是哪一个编码,还是两个都影响。

 req.ContentType = "application/x-www-form-urlencoded";

  req.Method = "POST";

  byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);

  req.ContentLength = bytes.Length;

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

  os.Write (bytes, 0, bytes.Length);

  os.Close ();

  System.Net.WebResponse resp = req.GetResponse();

  if (resp== null) return null;

  System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

  return sr.ReadToEnd().Trim();


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3