【问题标题】:URLConnection empty streamURLConnection 空流
【发布时间】:2013-07-03 08:11:20
【问题描述】:

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 获取响应流时出错(写入:EndWrite 失败):SendFailure

注意

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

【问题讨论】:

    标签: c# android url xamarin urlconnection


    【解决方案1】:

    让我们看看这个例子

    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();
       }
    

    【讨论】:

    • 试过这个,但它不起作用。不得不说应用是c#的。
    • 我看到你添加到Android术语,好的,让我们咨询我的新答案
    【解决方案2】:

    这是我的实用类

     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")
    

    【讨论】:

    • 我得到了这个异常:Sytem.Net.WebException 获取响应流时出错(写入:EndWrite 失败):SendFailure
    • 它适用于我的所有应用程序,我刚刚测试过,结果 = {tooltipHtml:" (26 m / 2 secs)",polylines:[{id:"route0",points:"yns ~H}fmz@i@X",levels:"BB",numLevels:4,zoomFactor:16}]}
    • 我希望我也能得到那个:(
    • 我猜你的电脑过滤掉了一些包,让我们关闭防火墙或其他安全程序或在另一台电脑上试试这个应用
    • 可能是 XamarinStudio 的问题,因为它以前可以工作,而我的计算机或代码中没有任何变化...
    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 2012-07-12
    • 2015-06-25
    • 1970-01-01
    • 2016-04-12
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多