HTTP GET in VB.NET

后端 未结 7 640
离开以前
离开以前 2020-11-27 15:25

What is the best way to issue a http get in VB.net? I want to get the result of a request like http://api.hostip.info/?ip=68.180.206.184

7条回答
  •  野性不改
    2020-11-27 16:13

    Try this:

    WebRequest request = WebRequest.CreateDefault(RequestUrl);
    request.Method = "GET";
    
    WebResponse response;
    try { response = request.GetResponse(); }
    catch (WebException exc) { response = exc.Response; }
    
    if (response == null)
        throw new HttpException((int)HttpStatusCode.NotFound, "The requested url could not be found.");
    
    using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
        string requestedText = reader.ReadToEnd();
    
        // do what you want with requestedText
    }
    

    Sorry about the C#, I know you asked for VB, but I didn't have time to convert.

提交回复
热议问题