0

URLConnection 流为空

我正在使用 Xamarin Studio 4.0.9 并使用以下代码:

Stream lResult = null;

URLConnection lConn = new URL(url).OpenConnection();

lResult = lConn.InputStream;

return lResult;

要从此网址获取流:http ://maps.google.com/maps?f=d&hl=en&saddr=52.37,9.74&daddr=52.370100224,9.739360256&ie=UTF8&0&om=0&output= dragdir 由于一些弱点它不起作用当我从中读取时,流是空的。有谁知道为什么?

更新

我也试过这个:

Uri lUri=new Uri(url);

WebRequest request=(WebRequest) WebRequest.Create(lUri); 

request.Method="GET";

WebResponse response = (WebResponse)request.GetResponse();

lResult=response.GetResponseStream();

但我得到了这个异常: System.Net.WebException Error getting response stream (Write: EndWrite failure): SendFailure

笔记

该应用程序是在 Xamarin Studio 4.0.9 中使用 c# 制作的

4

2 回答 2

0

让我们参考这个例子

URL url = new URL("http://maps.google.com/maps?f=d&hl=en&saddr=52.37,9.74&daddr=52.370100224,9.739360256&ie=UTF8&0&om=0&output=dragdir");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);//call function to read a stream
    finally {
     urlConnection.disconnect();
   }
于 2013-07-03T08:18:08.873 回答
0

这是我的实用类

 public class HttpConnection
    {
        public static string GetString(string url)
        {
            return GetString(url, Encoding.UTF8);
        }

        public static string GetString(string url, Encoding encode)
        {
            HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(url);
            oReq.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
            oReq.Method = "GET";
            ASCIIEncoding encoding = new ASCIIEncoding();
            HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse();
            StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), encode);
            String s = loResponseStream.ReadToEnd();
            loResponseStream.Close();

            return s.Replace(" ", " ");
        }
}

让我们像这样调用函数 HttpConnection.GetString(string url)

HttpConnection.GetString("http://maps.google.com/maps?f=d&hl=en&saddr=52.37,9.74&daddr=52.370100224,9.739360256&ie=UTF8&0&om=0&output=dragdir")
于 2013-07-03T08:43:28.413 回答