首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

怎么获得一url的响应然后建立连接读取字符串资源

2014-01-22 
求助:如何获得一url的响应然后建立连接读取字符串资源?java的代码如下[解决办法]public delegate void Htt

求助:如何获得一url的响应然后建立连接读取字符串资源?
java的代码如下

                                                                            
[解决办法]
public delegate void HttpHandle(object sender, HttpResultArgs args);
    public class HttpGet
    {
        public event HttpHandle HttpGetCompleted;
        public string url;

        public void GetRequestAsync(string url)
        {
            string resultStrings;
            HttpWebRequest req = HttpWebRequest.CreateHttp(url);
            req.BeginGetResponse((ar) =>
            {
                HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response;

                // End the get response operation 
                response = (HttpWebResponse)req2.EndGetResponse(ar);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                resultStrings = streamReader.ReadToEnd();
                if (HttpGetCompleted != null)
                {
                    HttpResultArgs args = new HttpResultArgs();
                    args.resultString = resultStrings;
                    HttpGetCompleted(this, args);
                }
            }
            , req);
        }
    }

[解决办法]

引用:

public delegate void HttpHandle(object sender, HttpResultArgs args);
    public class HttpGet
    {
        public event HttpHandle HttpGetCompleted;
        public string url;

        public void GetRequestAsync(string url)
        {
            string resultStrings;
            HttpWebRequest req = HttpWebRequest.CreateHttp(url);
            req.BeginGetResponse((ar) =>
            {
                HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response;

                // End the get response operation 
                response = (HttpWebResponse)req2.EndGetResponse(ar);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                resultStrings = streamReader.ReadToEnd();
                if (HttpGetCompleted != null)
                {
                    HttpResultArgs args = new HttpResultArgs();
                    args.resultString = resultStrings;
                    HttpGetCompleted(this, args);
                }
            }
            , req);
        }
    }
顶!

热点排行