我一直在研究一个项目,该项目通过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";
}
}
答案 0 :(得分:0)
使您的请求异步
public async Task<string> makerequest(string query){
}
然后在调用它时使用await修饰符
string response= await makerequest(yourstring)
异步执行此操作可以停止挂起的问题。重要的是要注意,您不必使调用方法与之异步,因为异步调用只能由异步方法进行。
答案 1 :(得分:0)
似乎https://restcountries.eu(我向其发送请求的其余服务器没有响应)
在使用其他http://countryapi.gear.host之类的其他服务器时,我的代码(原始)确实有效。