UploadValues 时 WebClient 异常
WebClient exception when UploadValues
我想这样调用 WebClient:
using (TimeoutWebClient client = new TimeoutWebClient())
{
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("email", email);
reqparm.Add("pass", password);
Debug.Log("5");
if (!string.IsNullOrEmpty(arg))
reqparm.Add(arg, "please");
Uri url = new Uri(URL);
byte[] responsebytes = client.UploadValues(url, "POST", reqparm);
Debug.Log("6");
string responsebody = Encoding.UTF8.GetString(responsebytes);
// Debug here
return responsebody;
}
}
catch (TimeoutException)
{
Debug.LogWarning("Timeout while request, retry !");
Debug.Log("7");
}
catch (Exception e)
{
Debug.LogError("Exception while request: " + e.Message + e.StackTrace);
return "Error";
}
但是当我 运行 这样做时,有时它会产生一个奇怪的异常,如下所示:
Exception while request: An error occurred performing a WebClient request. at System.Net.WebClient.UploadValues (System.Uri address, System.String method, System.Collections.Specialized.NameValueCollection data) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Net.WebClient:UploadValues (System.Uri,string,System.Collections.Specialized.NameValueCollection)
我真的不知道这是什么意思,所以如果有人遇到过这样奇怪的异常,请告诉我 :X 。 (我正在制作视频游戏,这是登录 post 请求)
PS:在Unity和.NET 2.0下不过差不多^^
编辑:这是完整的日志:
Message = The request timed out Help link = Source = System StackTrace = at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
编辑:这是 TimeoutWebClient class:
public class TimeoutWebClient : WebClient
{
private int _timeOut = 7000; // 7s
public int TimeOut
{
get
{
return _timeOut;
}
set
{
_timeOut = value;
}
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest webRequest = base.GetWebRequest(address);
webRequest.Timeout = _timeOut;
if (webRequest is HttpWebRequest)
{
(webRequest as HttpWebRequest).KeepAlive = false;
(webRequest as HttpWebRequest).Timeout = _timeOut; //(tried different values)
}
return webRequest;
}
}
这意味着您的请求连接时间超过 7 秒。您连接的 URL 不正确或服务器有问题。
为什么你不使用 HttpClient,
这是使用 httpClient 和 Json.net
的 Get/Post 请求的简单代码
public async Task<T> MakeHttpClientRequestASync<T>(string requestUrl, string authenticationToken,
Dictionary<string, string> requestContent, HttpMethod verb, Action<Exception> error)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoCache = true };
HttpResponseMessage response;
var returnVal = default(T);
try
{
if (verb == HttpMethod.Post)
{
response = await httpClient.PostAsync(requestUrl, new FormUrlEncodedContent(requestContent));
}
else
{
response = await httpClient.GetAsync(requestUrl);
}
var resultString = await response.Content.ReadAsStringAsync();
returnVal = JsonConvert.DeserializeObject<T>(resultString);
}
catch (Exception ex)
{
error(ex);
}
return returnVal;
}
好的,最后,我决定使用 Unity API 中的 WWW
class 而不是 .NET API,因为它似乎经过了优化。如果你想知道我使用的代码,这里是:
WWWForm form = new WWWForm();
form.AddField("email", email);
form.AddField("pass", password);
if (!string.IsNullOrEmpty(arg))
form.AddField(arg, "please");
WWW www = new WWW(URL, form);
// Wait until the request has been sent
yield return www;
if (www.isDone)
{
Debug.Log("WWW text = " + www.text);
if (callback(www.text))
{
// we are logged
}
else
{
// we arn't
}
}
我想这样调用 WebClient:
using (TimeoutWebClient client = new TimeoutWebClient())
{
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("email", email);
reqparm.Add("pass", password);
Debug.Log("5");
if (!string.IsNullOrEmpty(arg))
reqparm.Add(arg, "please");
Uri url = new Uri(URL);
byte[] responsebytes = client.UploadValues(url, "POST", reqparm);
Debug.Log("6");
string responsebody = Encoding.UTF8.GetString(responsebytes);
// Debug here
return responsebody;
}
}
catch (TimeoutException)
{
Debug.LogWarning("Timeout while request, retry !");
Debug.Log("7");
}
catch (Exception e)
{
Debug.LogError("Exception while request: " + e.Message + e.StackTrace);
return "Error";
}
但是当我 运行 这样做时,有时它会产生一个奇怪的异常,如下所示:
Exception while request: An error occurred performing a WebClient request. at System.Net.WebClient.UploadValues (System.Uri address, System.String method, System.Collections.Specialized.NameValueCollection data) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Net.WebClient:UploadValues (System.Uri,string,System.Collections.Specialized.NameValueCollection)
我真的不知道这是什么意思,所以如果有人遇到过这样奇怪的异常,请告诉我 :X 。 (我正在制作视频游戏,这是登录 post 请求)
PS:在Unity和.NET 2.0下不过差不多^^
编辑:这是完整的日志:
Message = The request timed out Help link = Source = System StackTrace = at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
编辑:这是 TimeoutWebClient class:
public class TimeoutWebClient : WebClient
{
private int _timeOut = 7000; // 7s
public int TimeOut
{
get
{
return _timeOut;
}
set
{
_timeOut = value;
}
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest webRequest = base.GetWebRequest(address);
webRequest.Timeout = _timeOut;
if (webRequest is HttpWebRequest)
{
(webRequest as HttpWebRequest).KeepAlive = false;
(webRequest as HttpWebRequest).Timeout = _timeOut; //(tried different values)
}
return webRequest;
}
}
这意味着您的请求连接时间超过 7 秒。您连接的 URL 不正确或服务器有问题。
为什么你不使用 HttpClient, 这是使用 httpClient 和 Json.net
的 Get/Post 请求的简单代码 public async Task<T> MakeHttpClientRequestASync<T>(string requestUrl, string authenticationToken,
Dictionary<string, string> requestContent, HttpMethod verb, Action<Exception> error)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoCache = true };
HttpResponseMessage response;
var returnVal = default(T);
try
{
if (verb == HttpMethod.Post)
{
response = await httpClient.PostAsync(requestUrl, new FormUrlEncodedContent(requestContent));
}
else
{
response = await httpClient.GetAsync(requestUrl);
}
var resultString = await response.Content.ReadAsStringAsync();
returnVal = JsonConvert.DeserializeObject<T>(resultString);
}
catch (Exception ex)
{
error(ex);
}
return returnVal;
}
好的,最后,我决定使用 Unity API 中的 WWW
class 而不是 .NET API,因为它似乎经过了优化。如果你想知道我使用的代码,这里是:
WWWForm form = new WWWForm();
form.AddField("email", email);
form.AddField("pass", password);
if (!string.IsNullOrEmpty(arg))
form.AddField(arg, "please");
WWW www = new WWW(URL, form);
// Wait until the request has been sent
yield return www;
if (www.isDone)
{
Debug.Log("WWW text = " + www.text);
if (callback(www.text))
{
// we are logged
}
else
{
// we arn't
}
}