“System.Net.HttpWebRequest"不包含“GetResponse"的定义 [英] 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse'
问题描述
我正在使用框架 4.5 在 Visual Studio 2013 中创建一个使用 Restful GET 方法的方法.这是 windows_phone_8(针对 Phone OS 8.0)应用程序.这是我的代码.
I am creating a method for consuming a restful GET method in Visual studio 2013, with framework 4.5. This is windows_phone_8 (targeting Phone OS 8.0) application. Here is my code.
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' 不包含定义'GetResponse' 和没有扩展方法 'GetResponse' 接受第一个可以找到System.Net.HttpWebRequest"类型的参数(你是缺少 using 指令或程序集引用?)
'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 一起工作得很好.
I don't know why it happens, the same code is working fine with Windows_application in the same environment.
更新:我也尝试过使用网络客户端方法
Update : I have tried with the web client method also
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();
又出现了一组错误......
Got another set of errors ...
错误 1 'System.Net.WebHeaderCollection' 不包含定义对于 'Add' 并且没有扩展方法 'Add' 接受第一个参数可以找到类型System.Net.WebHeaderCollection"(您是否缺少using 指令或程序集引用?)
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?)
错误 2 'System.Net.WebClient' 不包含定义'OpenRead' 和没有扩展方法 'OpenRead' 接受第一个可以找到System.Net.WebClient"类型的参数(你是缺少 using 指令或程序集引用?)
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?)
错误 3 'System.Net.HttpWebRequest' 不包含定义'GetResponse' 和没有扩展方法 'GetResponse' 接受第一个可以找到System.Net.HttpWebRequest"类型的参数(你是缺少 using 指令或程序集引用?)
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 的回答,我已将代码更改如下..
I have changed the code as follows, based on @Gavin' answer..
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();
}
}
但是控制回到调用事件,从下一行
But the control goes back to the calling event, from the following line
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
对此的任何帮助将不胜感激.
any help on this would be greatly appreciated.
答案:
我将代码更改如下,现在可以正常工作了..
I changed the code as follows and it is working now..
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 请求时还有一个问题.我试过的代码如下.
I have one more issue in processing POST request. The code I have tried is given below.
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 phone 8 应用程序中存在两个构建错误
This method is having two build errors in Windows phone 8 application
错误 1 'System.Net.HttpWebRequest' 不包含定义'GetRequestStream' 和无扩展方法 'GetRequestStream'接受System.Net.HttpWebRequest"类型的第一个参数可以找到了(您是否缺少 using 指令或程序集引用?)
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?)
错误 2 'System.Net.HttpWebRequest' 不包含定义'GetResponse' 和没有扩展方法 'GetResponse' 接受第一个可以找到System.Net.HttpWebRequest"类型的参数(你是缺少 using 指令或程序集引用?)
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 的一个子集.
WP8 supports a subset of .NET Framework 4.5.
您可以根据需要调整以下代码变体:
You can adapt the code variations below for your needs:
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)))
{
...
}
这篇关于“System.Net.HttpWebRequest"不包含“GetResponse"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!