WebRequest是不是不能给带参数的Uri啊?在线等,多谢!

bluemouse 2002-09-24 02:42:47
我试验了一下:如果用 http://www.domain.com/page.asp 则可以
如果用:http://www.domain.com/page.asp?name=value 则返回一个空的流

请大家指教如何解决?

我的程序如下:


public function GetWeb(strUrl as String) as String
Dim siteUri As New Uri(strUrl)
dim WebReq as System.Net.WebRequest = system.Net.WebRequest.Create(siteUri)
console.WriteLine(siteUri)
dim stm as System.IO.Stream
dim WebRes as System.Net.WebResponse = webreq.GetResponse
stm=WebRes.GetResponseStream
dim tr as System.Threading.Thread
tr.Sleep(5000)
dim buf(webres.ContentLength ) as Byte
dim counts as Integer = 0
dim total as Integer

do while total<webres.ContentLength

counts = stm.Read(buf,counts,webres.ContentLength-total)
total=total + counts

loop

GetWeb= system.Text.Encoding.Default.GetString(buf)

stm.Close
webres.Close


End Function
--
bluemouse <bluemouse@vip.sina.com>
...全文
213 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yangbinhz 2003-03-28
saucer(思归) 的方法好象只能用于asp.net,在控制台程序中该如何实现上述方法呀,急~!~~!~~~~
  • 打赏
  • 举报
回复
bluemouse 2002-09-26
改写了一下,如果不带参数,正常。但如果带参数,则标记的地方报错。我测试了一下,暂时没有发现因为我改写造成的错误。

请大家帮忙看看,分数好办。 :)


private function GetW(strUrl as String ) as String
dim uri as New Uri(strUrl)
dim res as System.Net.HttpWebResponse
dim strResult as String
dim paramList as String=uri.Query
try
dim req as system.Net.HttpWebRequest = system.Net.WebRequest.Create(Uri)


req.Method = "GET"
req.KeepAlive=True
req.Referer = "http://www.sina.com.cn/"
req.ContentType = "application/x-www-form-urlencoded"


dim reserved() as Char = {"?", "=", "&"}
dim SomeBytes() as Byte
dim UrlEncoded as New System.Text.StringBuilder

if len(paramList)>0 then

dim i as Integer=0
dim j as Integer

do while i<paramList.Length
j=paramList.IndexOfAny(reserved, i)
if j=-1 then
UrlEncoded.Append(system.web.HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)))
exit Do
End If
UrlEncoded.Append(system.Web.HttpUtility.UrlEncode(paramList.Substring(i, j-i)))
UrlEncoded.Append(paramList.Substring(j,1))
i = j+1
Loop
SomeBytes = system.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
req.ContentLength = SomeBytes.Length
dim newStream as System.IO.Stream
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 报错
newStream = req.GetRequestStream
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
newStream.Write(SomeBytes, 0, SomeBytes.Length)
newStream.Close()

else

'req.ContentLength = 0

End If

res = req.GetResponse()
dim ReceiveStream as System.IO.Stream = res.GetResponseStream()
dim encode as System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
dim sr as new system.IO.StreamReader ( ReceiveStream, encode )
dim read(256) as Char
dim count as Integer = sr.Read(read, 0, 256)
do while count>0
dim str as New String(read,0,count)
strResult=strResult + str
count = sr.Read(read, 0, 256)
Loop

catch e as Exception
console.WriteLine (e.ToString )

finally



End Try

return strResult

End Function
  • 打赏
  • 举报
回复
saucer 2002-09-26
the following works for me:

imports System
imports System.Net
imports System.IO
imports System.Text

class TestWeb

public shared sub Main()
dim lcUrl as string = "http://www.csdn.net/expert/Forum.asp?Typenum=2&roomid=33"

dim loHttp as HttpWebRequest = Ctype( WebRequest.Create(lcUrl), HttpWebRequest)

loHttp.Timeout = 10000

loHttp.UserAgent = "Code Sample Web Client"

dim loWebResponse as HttpWebResponse = CTYpe(loHttp.GetResponse(),HttpWebResponse)

dim enc as Encoding = Encoding.Default

dim loResponseStream as new StreamReader(loWebResponse.GetResponseStream(),enc)

dim lcHtml as string = loResponseStream.ReadToEnd()

loWebResponse.Close()

loResponseStream.Close()

Console.WriteLine(lcHtml)

end sub
end class
  • 打赏
  • 举报
回复
bluemouse 2002-09-25
先给一半,等我改成了vb.net的,再给另外的。 :)
  • 打赏
  • 举报
回复
spring_ok 2002-09-24
可以的。用HttpWebRequest.GetRequestStream()来添加参数。

public static string getPage(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";

try
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.KeepAlive = true;
req.Referer = "https://www.nsldsfap.ed.gov/secure/lnhist.asp";
req.ContentType = "application/x-www-form-urlencoded";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
req.CookieContainer.SetCookies(new Uri(url),cookieheader);
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;

if (paramList != null)
{
int i=0, j;
while(i<paramList.Length)
{
j=paramList.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
UrlEncoded.Append(paramList.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}


res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
strResult = e.ToString();
}
finally
{
if ( res != null )
{
res.Close();
}
}

return strResult;
}

  • 打赏
  • 举报
回复
bluemouse 2002-09-24
upup
  • 打赏
  • 举报
回复
bluemouse 2002-09-24
微软专家在不在?
  • 打赏
  • 举报
回复
bluemouse 2002-09-24
sqhua理解错了,我说的是取其他网站的叶面的问题
  • 打赏
  • 举报
回复
buaawjh 2002-09-24
强烈关注
  • 打赏
  • 举报
回复
sqhua 2002-09-24
Request.QueryString["OperateCode"] ;
  • 打赏
  • 举报
回复
bluemouse 2002-09-24
up
  • 打赏
  • 举报
回复
发帖
.NET社区
加入

6.0w+

社区成员

.NET技术交流专区
社区管理员
  • ASP.NET
  • R小R
  • 喵叔哟
帖子事件
创建了帖子
2002-09-24 02:42
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。