gpt4 book ai didi

c# - 在 Windows Phone 的 C# 中使用 GetResponse 时遇到问题

转载 作者:行者123 更新时间:2023-12-03 19:25:59 26 4
gpt4 key购买 nike

当我使用

 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

我收到一条错误消息,指出

'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? 

我添加了以下引用,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp23.Resources;
using System.IO;
using System.Text;
using System.Threading;

我是不是漏掉了什么??或者我应该怎么做才能使它正常工作!

最佳答案

在 Windows Phone 中,您需要执行每个可能需要 50 毫秒异步时间的操作。由于 webrequest 可能需要更长的时间,因此微软从类中删除了 sync 方法。相反,您需要使用异步方法:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://www.google.com"));
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
using(HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
{
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//execute UI stuff on UI thread.
Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
}
}
}

关于c# - 在 Windows Phone 的 C# 中使用 GetResponse 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422190/

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