当前位置: 代码迷 >> ASP.NET >> 一段POST提交的.NET代码,报错,请大家帮忙查看,多谢
  详细解决方案

一段POST提交的.NET代码,报错,请大家帮忙查看,多谢

热度:8147   发布时间:2013-02-25 00:00:00.0
一段POST提交的.NET代码,报错,请大家帮忙查看,谢谢
服务端没有错误,用JAVA开发的客户端调用,正常调用。
下面是C#的发送端,代码如下:
  System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://test.aspx");
  req.Method = "POST";
  req.UserAgent = "MSIE";
  req.ContentType = "application/x-www-form-urlencoded";  
  byte[] SomeBytes = System.Text.Encoding.GetEncoding("utf-8").GetBytes("name=commu&id=1");//传递的值 
  req.ContentLength = SomeBytes.Length;
  System.IO.Stream newStream = req.GetRequestStream();//把传递的值写到流中  
  newStream.Write(SomeBytes, 0, SomeBytes.Length);
  newStream.Close();//必须要关闭请求
  System.Net.HttpWebResponse httpWebResponse = null;
  httpWebResponse = (System.Net.HttpWebResponse)req.GetResponse();
  System.IO.Stream s = httpWebResponse.GetResponseStream();
  System.IO.StreamReader reader = new System.IO.StreamReader(s, System.Text.Encoding.Default);
  string respHTML = reader.ReadToEnd();
  Response.Write(respHTML);
  s.Close();


报错:
 “s.Length”引发了“System.NotSupportedException”类型的异常
 “s.Position”引发了“System.NotSupportedException”类型的异常

------解决方案--------------------------------------------------------
s.Length和s.Position在哪呢,错误提示是不支持,s没有这两个属性吧
------解决方案--------------------------------------------------------
public virtual string HttpPost(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = true;

StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;

try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();

response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
response = null;
}

return responseStr;
}

------解决方案--------------------------------------------------------
Length和s.Position在哪呢,错误提示是不支持,s没有这两个属性吧
------解决方案--------------------------------------------------------
s在哪里啊?,s在哪里?
------解决方案--------------------------------------------------------
定义的s没有这两个属性
  相关解决方案