簡體   English   中英

request.GetResponse()似乎掛斷了我的程序

[英]request.GetResponse() seems to hang up my program

我一直在研究通過httprequests從其他服務器獲取結果的項目。 在這種情況下,我嘗試並成功(昨天,相同的代碼也起作用。)搜索國家/地區並將其標志設置為dipslay。

我已經縮小了函數“ makeRequest”的范圍。

通過掛斷,我的意思是我什至不能通過單擊右上角的“ x”來關閉表單,而必須通過單擊“停止調試”來強制退出。

任何提示,我在做什么錯了或出錯了,將不勝感激。

表單使用的代碼:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ApiQueryHandler
{
abstract class QueryCreator
{

    static public string createQueryLandcode(String country)
    {
        string queryStandaard = "https://restcountries.eu/rest/v2/name/";
        string queryLandCode = "?fields=alpha2Code";
        string query = queryStandaard + country + queryLandCode;
        return query;
    }
    static public string makeRequest(string query)
    {
        WebRequest request = WebRequest.Create(query);
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();
        return responseFromServer;
    }
    static public string createQueryFlag(string country)
    {
        string queryStandaard = "https://restcountries.eu/rest/v2/name/";
        string queryFlag = "?fields=flag";
        string query = queryStandaard + country + queryFlag;
        return query;
    }
    static public string getSVGFlagUrl(string queryResponseFlag)
    {
        String beginUrl = queryResponseFlag.Substring(10);
        int endUrl = beginUrl.IndexOf('"');
        String url = queryResponseFlag.Substring(10,endUrl);
        return url;
    }
    static public string getPNGFlagUrl(string landCode)
    {
        string PNGUrl = "https://www.countryflags.io/"+landCode+"/flat/64.png";
        return PNGUrl;
    }
    static public string getCode(string queryResponseCode)
    {
        String beginCode = queryResponseCode.Substring(16);
        int endCode = beginCode.IndexOf('"');
        String code = queryResponseCode.Substring(16, endCode);
        return code;
    }
    static public string getCodeShort(string queryResponseCode)
    {
        String beginCode = queryResponseCode.Substring(10);
        int endCode = beginCode.IndexOf('"');
        String code = queryResponseCode.Substring(10, endCode-1);
        return code;
    }
}

}

表單代碼:

private void textBoxCounrty_TextChanged(object sender, EventArgs e)
    {
        string country = textBoxCountry.Text;
        string queryLandcode = QueryCreator.createQueryLandcode(country);
        string queryFlag = QueryCreator.createQueryFlag(country);
        try
        {

            string responseLandcode = QueryCreator.makeRequest(queryLandcode);
            string responseFlag = QueryCreator.makeRequest(queryFlag);
            string SVGUrl = QueryCreator.getSVGFlagUrl(responseFlag);
            labelURLQuery.Text = queryLandcode;
            string processedLandcode = QueryCreator.getCode(responseLandcode);
            string PNGUrl = QueryCreator.getPNGFlagUrl(processedLandcode);
            richTextBoxServerResponse.Text = responseLandcode+"\n\n"+processedLandcode+"\n\n"+PNGUrl+"\n\n"+SVGUrl;
            pictureBoxFlag.Load(PNGUrl);
            labelERROR.Text = "";
        }
        catch (WebException)
        {
            labelERROR.Text = "404 not found";
        }
        catch (ArgumentException)
        {
            labelERROR.Text = "404 not found";
        }

    }

使您的請求異步

public async Task<string> makerequest(string query){

}

然后在調用它時使用await修飾符

string response= await makerequest(yourstring)

異步執行此操作可以停止掛起的問題。 重要的是要注意,您不必使調用方法與之異步,因為只能通過異步方法進行異步調用。

似乎https://restcountries.eu (我向其發送請求的其余服務器沒有響應)

當使用其他http://countryapi.gear.host等其他服務器時,我的代碼(原始)可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM