WebClient Client = new WebClient();
String Url = "http://***/check.asp?user="+user;   //user是textbox1.text.trim()
Byte[] data=new byte[1024];
try
{
     WebRequest myreq = WebRequest.Create(Url );
     data = Client.DownloadData(Url);
}
catch (WebException expc)
{
     MessageBox.Show(expc.Message, "网络错误");
}
return Encoding.Default.GetString(data);check.asp用GET方法得到user  执行一个查询 如果存在这个user 返回true 否则返回false
----------问题来了
我用IE访问http://***/check.asp?user=张3,结果是true
同样的URL 这里return Encoding.Default.GetString(data)得到的却是false 何解??会不会是因为执行的时候吧后面的 ?user=张3 给丢了??
要怎样才能拿到正确结果

解决方案 »

  1.   

    WebClient.QueryString Property 
    你可以看看,我是新手,但是感觉这个对你有用下面是msdn上的例子: string uriString = "http://www.contoso.com/search";
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Create a new NameValueCollection instance to hold the QueryString parameters and values.
    NameValueCollection myQueryStringCollection = new NameValueCollection();
    Console.Write("Enter the word(s), separated by space character to search for in " +  uriString + ": ");
    // Read user input phrase to search for at uriString.
    string searchPhrase = Console.ReadLine();
    if (searchPhrase.Length > 1)
    // Assign the user-defined search phrase.
    myQueryStringCollection.Add("q",searchPhrase);
    else
    // If error, default to search for 'Microsoft'.
    myQueryStringCollection.Add("q","Microsoft");
    // Assign auxilliary parameters required for the search.
    Console.WriteLine("Searching " + uriString + " .......");
    // Attach QueryString to the WebClient.
    myWebClient.QueryString = myQueryStringCollection;
    // Download the search results Web page into 'searchresult.htm' for inspection.
    myWebClient.DownloadFile (uriString, "searchresult.htm");
    Console.WriteLine("\nDownload of " + uriString + " was successful. Please see 'searchresult.htm' for results.");
      

  2.   

    做了个测试,果然可以,
    要用System.Collections.Specialized;            String Url = "http://localhost/NetSite/Default.aspx";               WebClient Client = new WebClient();
                NameValueCollection nvc = new NameValueCollection();
                nvc.Add("str", "nihaohaohodjsajdlaj");
                Client.QueryString = nvc;
                Byte[] data = new byte[1024];
                try
                {
                    WebRequest myreq = WebRequest.Create(Url);                data = Client.DownloadData(Url);
                }
                catch (WebException expc)
                {
                    MessageBox.Show(expc.Message, "网络错误");
                }
                textBox1.Text= Encoding.Default.GetString(data);
      

  3.   

    找到了  问题不在这里
    刚刚吧那个页面加了点东西  让他返回错误的同时 返回传过来的参数具体是什么
    结果在程序里得到的是一堆乱码String Url = "http://***/check.asp?user=张3"
    程序里访问这个地址
    页面返回的是 "Error"+乱码   那么页面在执行查询的时候肯定查的是这个乱码了  晕结贴  不知道是怎么转的  好像中文就会出这个问题
    只有改数据库了 user=Md5(张3)再错就无语了