gpt4 book ai didi

windows-phone-8 - “System.Net.HttpWebRequest”不包含 'GetResponse' 的定义

转载 作者:行者123 更新时间:2023-12-04 07:00:21 27 4
gpt4 key购买 nike

我正在使用框架 4.5 在 Visual Studio 2013 中创建一个使用 Restful GET 方法的方法。这是 windows_phone_8(针对 Phone OS 8.0)应用程序。这是我的代码。

static string HttpGet(string url)
{
HttpWebRequest req = WebRequest.Create(url)
as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}

但我收到如下构建错误

'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)



我不知道为什么会发生这种情况,相同的代码在相同的环境中与 Windows_application 一起正常工作。

更新:我也尝试过使用 Web 客户端方法
 WebClient client = new WebClient();

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead("http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();

得到另一组错误...

Error 1 'System.Net.WebHeaderCollection' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Net.WebHeaderCollection' could be found (are you missing a using directive or an assembly reference?)

Error 2 'System.Net.WebClient' does not contain a definition for 'OpenRead' and no extension method 'OpenRead' accepting a first argument of type 'System.Net.WebClient' could be found (are you missing a using directive or an assembly reference?)

Error 3 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)



更新 2:

我根据@Gavin 的回答更改了代码如下。
static async void HttpGet(string url)
{
Uri uri = new Uri(url);
string result = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}
}

但是控制返回到调用事件,从以下行
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))

对此的任何帮助将不胜感激。

答案:

我按如下方式更改了代码,现在可以正常工作了..
public async Task<string> httpRequest(string url)
{
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string received;

using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{

received = await sr.ReadToEndAsync();
}
}
}

return received;
}

调用部分如下...
private async void Button_Click(object sender, RoutedEventArgs e)
{
string uriString = "http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor";
var response = await httpRequest(uriString);
}

更新 3:

我在处理 POST 请求时还有一个问题。我试过的代码如下。
static string HttpPost(string url, string[] paramName, string[] paramVal)
{
HttpWebRequest req = WebRequest.Create(new Uri(url))
as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

// Build a string with all the params, properly encoded.
// We assume that the arrays paramName and paramVal are
// of equal length:
StringBuilder paramz = new StringBuilder();
for (int i = 0; i < paramName.Length; i++)
{
paramz.Append(paramName[i]);
paramz.Append("=");
paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
paramz.Append("&");
}

// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;

// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}

// Pick up the response:
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}

return result;
}

此方法在 Windows 手机 8 应用程序中有两个构建错误

Error 1 'System.Net.HttpWebRequest' does not contain a definition for 'GetRequestStream' and no extension method 'GetRequestStream' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)

Error 2 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)



谢谢
塞巴斯蒂安

最佳答案

WP8 支持 .NET Framework 4.5 的一个子集。

您可以根据需要调整以下代码变体:

WebRequest request = WebRequest.Create(url);
return Task.Factory.FromAsync(request.BeginGetResponse, result =>
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
...
}

或者
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
...
}

关于windows-phone-8 - “System.Net.HttpWebRequest”不包含 'GetResponse' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com