using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net ;
using System.IO ;
using System.Text ;
using System.Text.RegularExpressions;
using System.Web ;namespace ConsoleApplication3
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string url="http://61.156.3.58/test.jsp";
string md5="";
string content="COMMANDID=01&CORPID=40068887807&CPPW="+md5+"&SOURCEADDRFLAG=1&PHONE=13156160733&SENDTIME=2006-10-20 17:25:00&TITLE=smstitle&CONTENT=smstest!";
Class1 post=new Class1 ();
string con=post.postget (url,content);

}
public string  postget(string url, string content)
{
try 
{
Encoding encoding = Encoding.GetEncoding("GB2312");
byte[] data = encoding.GetBytes(content);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// 发送数据
newStream.Write(data,0,data.Length);
newStream.Close();
//接收数据
HttpWebResponse  webResponse=(HttpWebResponse)myRequest.GetResponse(); 
//Console.WriteLine ("aa");
Stream stream=webResponse.GetResponseStream();
//System.IO.StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));
StreamReader streamReader=new StreamReader (stream);
string content1 = streamReader.ReadToEnd();
// 关闭相关对象
//Console.WriteLine (content1);
return content1;
streamReader.Close();
webResponse.Close();
}
catch (Exception e)
{
Console.WriteLine ("error:{0}"+e.ToString ());
}
}
}
}我想获得一个返回的值怎么就是不行呢,原来返回是void,现在怎么总出错呢.咋回事??

解决方案 »

  1.   

    方法 并非所有的代码都有返回值.
    检测到无法访问的代码.
    streamReader.Close();
    webResponse.Close();
      

  2.   

    改成
    catch (Exception e)
    {
    Console.WriteLine ("error:{0}"+e.ToString ());
    }
                                finally{
    streamReader.Close();
    webResponse.Close();
                                 }
      

  3.   

    怎么还是出错,说streamReader.Close();webResponse.Close();是不是缺少using引用.或程序集引用...我加了一个引用system.dll.结果还是不行.到底是怎么回事情.我用的是2003
      

  4.   

    streamReader和webResponse要定义在try的外面