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 WindowsApplication5
{
public class Post
{
public static void Main(string [] args)
{
Post zhucheng=new Post();
zhucheng.zhuchengxu(); }
public void zhuchengxu()
{
try
{
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="COMMANDID=05";
byte[]  data = encoding.GetBytes(postData);
// Prepare web request
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create("http://61.156.3.58/test.jsp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
Stream stream=webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));
string content = streamReader.ReadToEnd();
// 关闭相关对象
streamReader.Close();
webResponse.Close();
}

catch(Exception ed ) 

Console.WriteLine ( "发生" + ed.ToString()+"错误!" ) ; 
}
}
}
}
程序总出错误

解决方案 »

  1.   

    HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
    改成:
    HttpWebResponse webResponse = (HttpWebResponse) myRequest.GetResponse();
      

  2.   

    Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
    for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
    Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.test2.cs(37,53): error CS0103: The name 'webRequest' does not exist in the
            current context
    这是错误提示,要学会调试的方法....
      

  3.   

    看不出有什么问题?----------------------------------
    http://blog.cngator.com
      

  4.   

    用的是2003.错误的提示是找不到类型或命名空间,当初找啊找就是不知道错误在什么地方。错误的提示内容是不是因为版本不一样而不一样啊?刚用.net 第四天.很多问题.
      

  5.   

    模拟POST
    用 webclient 最方便了.
    string urlString="http://www.csdn.net";WebClient client=new WebClient();
    NameValueCollection formVars = new NameValueCollection();formVars.Add('test1','value1');
    formVars.Add('test2','value2');byte[] responseArray = this.client.UploadValues(urlString,"POST", formVars);
    string strBody = Encoding.UTF8.GetString(responseArray);
      

  6.   

    我就没有装过VS2003对我来说太大了...我就是用命令行把你的代码复制到  test2.cs然后 
    csc test.cs就提示上面的错误了....
      

  7.   

    哈哈,现在刚接触这个.net第四天,真实麻烦啊.也没有做过开发,
    从网上搜了一些资料,不是很会啊.现在要做一个这个接口的程序,真是很费力气.
      

  8.   

    mrshelly(Shelly) 真是高手,嘿嘿
      

  9.   

    package com.easecom.tomcatscan;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2006</p>
     * <p>Company: </p>
     * @author easecom dongyf
     * @version 1.0
     */
    import java.io.*;
    import java.net.*;public class postdemo {
      public postdemo() {
      }  public static void main(String[] args) {    try {
          URL url = new URL("http://61.156.3.58/test.jsp"); //测试url,会把您发的参数再返回
         // URL url = new URL("http://61.156.3.58/SendSms"); //实际url
          URLConnection connection = url.openConnection();      connection.setDoOutput(true);      OutputStreamWriter out = new OutputStreamWriter(connection.
              getOutputStream(), "GB2312");
          out.write("COMMANDID=05&CORPID=40068887807&CPPW=passwordaftermd5&SOURCEADDRFLAG=1&PHONE=13156160733&SENDTIME=2006-10-20 17:25:00&TITLE=smstitle&CONTENT=smstest!");      out.flush();
          out.close();
          DataInputStream in = new DataInputStream(connection.getInputStream());
          String inline = "";
          while ( (inline = in.readLine()) != null) {
            System.out.println(inline);
          }
          in.close();
        }
        catch (IOException ex2) {
          System.out.println(" exe IOException:" + ex2.toString());    }  }}
    现在在比着这个 写.net程序,里边的东西不会的太多.还得读.xml的文档,现在还不会呢.具体到哪个编码也不是很清楚.我就比较纳闷为什么.net 都用accii呢.真是麻烦啊.
      

  10.   

    我就比较纳闷为什么.net 都用accii呢.真是麻烦啊.
    这个是传统 是喜欢,好习惯
      

  11.   

    现在所有项目均采用UTF8编码.....
      

  12.   

    我以前开发的代码的一部分HttpWebRequest httpWRQ = (HttpWebRequest)WebRequest.Create(_Page);
                HttpWebResponse httpWRP = (HttpWebResponse)httpWRQ.GetResponse();
                Stream dataStream = httpWRP.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("GB2312"));
                string s_HtmlData = reader.ReadToEnd();
                string s_FilePathAll = @"c:\Log\LogSource1.txt";
                StreamWriter swAll = File.CreateText(s_FilePathAll);
                swAll.Write(s_HtmlData);
                swAll.Close();另外可以去我博客看看
    里面我上传了一个开发的网页抓取程序