我创造一个HttpWebRequest目从另一个aspx页节省的响应流到我的数据存储。Url我使用的创建HttpWebRequest对象具有的查询要呈现了正确的输出。当我浏览的网页使用任何旧的浏览器,它使得正确。当我试试来检索出的流用HttpWebResponse.GetResponseStream()它呈现我的内在的错误检查。

为什么会正确地呈现在浏览器,但不使用HttpWebRequest和HttpWebResponse的对象?

这里是源代码:

代码后面的目标网页:

protected void PageLoad(object sender, EventsArgs e)
{
   string output = string.Empty;

   if(Request.Querystring["a"] != null)
   {
      //generate output
      output = "The query string value is " + Request.QueryString["a"].ToString();
   }
   else
   {
      //generate message indicating the query string variable is missing
      output = "The query string value was not found";
   }

   Response.Write(output);
}

代码的网页创建HttpWebRequest对象

string url = "http://www.mysite.com/mypage.aspx?a=1";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url)

//this if statement was missing from original example
if(User.Length > 0)
{
    request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");
    request.PreAuthenticate = true;
}

request.UserAgent = Request.UserAgent;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream resStream = response.GetResponseStream();  
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(resStream, encode, true, 2000);
int count = readStream.Read(read, 0, read.Length);
string str = Server.HtmlEncode(" ");

while (count > 0)
{
    // Dumps the 256 characters on a string and displays the string to the console.
    string strRead = new string(read, 0, count);
    str = str.Replace(str, str + Server.HtmlEncode(strRead.ToString()));
    count = readStream.Read(read, 0, 256);
}

// return what was found
result = str.ToString();

resStream.Close();
readStream.Close();

更新

@David McEwing-我创造的HttpWebRequest与全页名称。页面仍然是产生错误的输出。我更新代码样本的目标网页,以证明正是我在做什么。

@克里斯*热闹-我不是向一个错误,我产生消息表示query string值没有被发现。我更新的源代码的例子。

更新1:

我试图使用小提琴手来追踪HttpWebRequest并没有出现在该网的会议历史上的窗口。我失去了一些东西在我的源代码,以获得一个完整的网络的请求,并响应。

更新2:

我不包括以下一段代码在我的例子,它是罪魁祸首,引起的问题。我设置的 Credentials 酒店的 HttpWebRequest 有服务的帐户,而不是我的广告的帐户,这是造成问题。

我更新我的源代码的例子

有帮助吗?

解决方案 3

我需要更换下列行代码:

request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");

有:

request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

其他提示

什么样的网络服务器你都用?我可以记得有一次我去的时候做一些与IIS有一个问题之间的重定向 http://example.com/http://example.com/default.asp 下降的query string.

也许是运行提琴手(或嗅探器议定书》),并看看有没有东西发生的事情,你是不是期待。

还检查,如果通过在充分的网页名称的工作。如果它不上几乎是肯定的问题。

你可以尝试使用 AllowAutoRedirect 酒店的HttpRequestObject.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top