代码尝试通过http请求从php脚本下载文本。由于代码应该在带有Windows IoT的Raspberry Pi上运行,我必须使用GetResponseAsync()。如果我在没有凭据的情况下使用它(Http源是公共的),它工作正常。当我添加NetworkCredential的东西时,我得到你在图片中看到的异常。 Screenshot 最后,我希望能够从通过.htaccess保护的http网站加载数据。我希望任何人都有这方面的经验!
namespace HttpGetBeisp1Hta {
public sealed partial class MainPage : Page {
public MainPage() {
InitializeComponent();
}
private async void ClickMe_Click(object sender, RoutedEventArgs e)
{
try {
await HoleDaten();
}
catch(Exception ex) {
textBlock.Text = ex.ToString();
}
}
private async Task HoleDaten() {
NetworkCredential myCred = new NetworkCredential("user", "pw");
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("https://..'my secret uri'../getSave.php"), "Basic", myCred);
WebRequest request = WebRequest.Create("https://..'my secret uri'../getSave.php");
request.Credentials = myCache;
WebResponse response = await request.GetResponseAsync();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
textBlock.Text = responseFromServer;
}
}
}
答案 0 :(得分:0)
发现它!
我手动修改了标题。用户名和密码需要在base64中添加。使用转换器并用“Hans:Mueller”喂它:
private async Task HoleDaten() {
WebRequest request = WebRequest.Create("https:// ... ll.php");
request.Headers["Authorization"] = "Basic SGFuczpNdWVsbGVy";
WebResponse response = await request.GetResponseAsync();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
textBlock.Text = responseFromServer;
}