怎么获得一url的响应然后建立连接读取字符串资源
求助:如何获得一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); } } 顶!