【发布时间】:2011-08-27 20:25:31
【问题描述】:
https://qrng.physik.hu-berlin.de/ 的页面提供高比特率量子数生成器 Web 服务,我正在尝试访问该服务。
但是我没能做到。这是我当前的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using S=System.Text;
using System.Security.Cryptography;
using System.IO;
namespace CS_Console_App
{
class Program
{
static void Main()
{
System.Net.ServicePointManager.Expect100Continue = false;
var username = "testuser";
var password = "testpass";
System.Diagnostics.Debug.WriteLine(Post("https://qrng.physik.hu-berlin.de/", "username="+username+"&password="+password));
Get("http://qrng.physik.hu-berlin.de/download/sampledata-1MB.bin");
}
public static void Get(string url)
{
var my_request = System.Net.WebRequest.Create(url);
my_request.Credentials = System.Net.CredentialCache.DefaultCredentials;
var my_response = my_request.GetResponse();
var my_response_stream = my_response.GetResponseStream();
var stream_reader = new System.IO.StreamReader(my_response_stream);
var content = stream_reader.ReadToEnd();
System.Diagnostics.Debug.WriteLine(content);
stream_reader.Close();
my_response_stream.Close();
}
public static string Post(string url, string data)
{
string vystup = null;
try
{
//Our postvars
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
//Initialisation, we use localhost, change if appliable
System.Net.HttpWebRequest WebReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
System.Net.HttpWebResponse WebResp = (System.Net.HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
vystup = _Answer.ReadToEnd();
//Congratulations, you just requested your first POST page, you
//can now start logging into most login forms, with your application
//Or other examples.
}
catch (Exception ex)
{
throw ex;
}
return vystup.Trim() + "\n";
}
}
}
当我尝试在 http://qrng.physik.hu-berlin.de/download/sampledata-1MB.bin 上执行获取请求时遇到 403 禁止错误。
在调试 abit 之后,我意识到即使我提供了有效的用户名和密码,在我的 POST 请求之后发送的响应 html 表明我在我的 POST 请求之后实际上没有登录到系统。
有谁知道为什么会这样,我该如何解决它来调用服务?
撞。任何人都可以让它工作还是该网站只是一个骗局?
【问题讨论】:
-
您确定带有用户名和密码的 URL 实际上是登录该站点的有效方式吗?
-
您似乎没有使用定义的变量作为用户名和密码?
-
@Daniel Hilgarth 是的,它是有效的。您可以尝试创建一个帐户并自己添加值。
-
@Michel 我实际上并没有发布实际的用户名和密码,但你可以放心,它是用实际的替换的
-
@Daniel Hilgarth 我创建了一个测试用户。用户名是 testuser,密码是 testpass。 (如果您需要该帐户进行测试)
标签: c# vb.net web-services web-applications