URLConnection Stream为空
我正在使用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#进行的。
答案 0 :(得分: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();
}
答案 1 :(得分:0)
这是我的util类
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")