.net 2.0 webrequest getresponse 方法404进抛异常,但我想获取response的流 应该怎么办 高人救命啊
高人拜谢了问题已解决catch(WebExceptionex)ex.Response属性中有是反回的流http://msdn.microsoft.com/zh-cn/li...
高人 拜谢了
问题已解决 catch (WebException ex) ex.Response属性中有是反回的流
http://msdn.microsoft.com/zh-cn/library/system.net.webexception.response.aspx
还得自己动手啊 展开
问题已解决 catch (WebException ex) ex.Response属性中有是反回的流
http://msdn.microsoft.com/zh-cn/library/system.net.webexception.response.aspx
还得自己动手啊 展开
2个回答
展开全部
给你一段我在用的函数吧,直接调用就是了:
private string WebGet(string url,string key1,string value1,string useEncodeing="UTF8",string EndString="")
{
string res = "";
//GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:
try
{
Encoding myEncoding;
if (useEncodeing == "UTF8")
myEncoding = Encoding.UTF8;// Encoding.GetEncoding("gb2312");//utf8 Encoding.UTF8; //
else if (useEncodeing == "gb2312")
myEncoding = Encoding.GetEncoding("gb2312");
else
myEncoding = Encoding.Default;
string para1 = key1;
string para1vlaue = value1;
string address="";
if(key1.Length>0)
address = url + HttpUtility.UrlEncode(para1, myEncoding) + "=" + HttpUtility.UrlEncode(para1vlaue, myEncoding);
else
address = url + HttpUtility.UrlEncode(para1vlaue, myEncoding);
if (EndString.Length > 0)
address = address + EndString;
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
StreamReader reader = new StreamReader(wr.GetResponseStream(), myEncoding);//Encoding.UTF8
res = reader.ReadToEnd();
}
}
catch { }
return res;
}
private string WebGet(string url,string key1,string value1,string useEncodeing="UTF8",string EndString="")
{
string res = "";
//GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:
try
{
Encoding myEncoding;
if (useEncodeing == "UTF8")
myEncoding = Encoding.UTF8;// Encoding.GetEncoding("gb2312");//utf8 Encoding.UTF8; //
else if (useEncodeing == "gb2312")
myEncoding = Encoding.GetEncoding("gb2312");
else
myEncoding = Encoding.Default;
string para1 = key1;
string para1vlaue = value1;
string address="";
if(key1.Length>0)
address = url + HttpUtility.UrlEncode(para1, myEncoding) + "=" + HttpUtility.UrlEncode(para1vlaue, myEncoding);
else
address = url + HttpUtility.UrlEncode(para1vlaue, myEncoding);
if (EndString.Length > 0)
address = address + EndString;
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
StreamReader reader = new StreamReader(wr.GetResponseStream(), myEncoding);//Encoding.UTF8
res = reader.ReadToEnd();
}
}
catch { }
return res;
}
追问
WebResponse wr = req.GetResponse()
这一步已经抛异常 下面是执行不了的
高手有没有好的处理办法
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询