WinForm的WebRequest问题

Error_Code 2008-03-14 04:29:25

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 给丢了??
要怎样才能拿到正确结果
...全文
171 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Error_Code 2008-03-14
  • 打赏
  • 举报
回复
找到了 问题不在这里
刚刚吧那个页面加了点东西 让他返回错误的同时 返回传过来的参数具体是什么
结果在程序里得到的是一堆乱码

String Url = "http://***/check.asp?user=张3"
程序里访问这个地址
页面返回的是 "Error"+乱码 那么页面在执行查询的时候肯定查的是这个乱码了 晕

结贴 不知道是怎么转的 好像中文就会出这个问题
只有改数据库了 user=Md5(张3)再错就无语了
aiht 2008-03-14
  • 打赏
  • 举报
回复
做了个测试,果然可以,
要用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);
aiht 2008-03-14
  • 打赏
  • 举报
回复
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.");

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧