远程服务器返回错误:(403)禁止。错误?

时间:2011-09-13 06:15:06

标签: stream httpwebrequest picasa

下面是我的代码..我收到错误 - “远程服务器返回错误:(403)禁止。”..:

           TimeSpan t = DateTime.Now - new DateTime(1970, 1, 1);

           string content = @"<entry xmlns='http://www.w3.org/2005/Atom'   
            xmlns:media='http://search.yahoo.com/mrss/'  
            xmlns:gphoto='http://schemas.google.com/photos/2007' >
            <title type='text'>Trip To Italy</title>
            <summary type='text'>This was the recent trip I took to Italy.</summary>
            <gphoto:location>Italy</gphoto:location>
            <gphoto:access>public</gphoto:access>
            <gphoto:timestamp>" + t.Milliseconds.ToString() + @"</gphoto:timestamp>
            <media:group><media:keywords>italy</media:keywords></media:group>
            <category scheme='http://schemas.google.com/g/2005#kind' 
             term='http://schemas.google.com/photos/2007#album'></category>
            </entry>";
        ServicePointManager.ServerCertificateValidationCallback = new 
        RemoteCertificateValidationCallback(delegate(object sender2, X509Certificate 
        certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        });
        string url = "https://www.picasaweb.google.com/data/feed/api/user/default";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/atom+xml";
        request.Headers.Add(HttpRequestHeader.Authorization, "AuthSub token=\"" + 
        Session["token"].ToString() + "\"");
        request.Headers.Add("GData-Version", "2.0");
        byte[] send = System.Text.UTF8Encoding.UTF8.GetBytes(content);
        request.ContentLength = send.Length;
        int bytesRead = 0;
        Stream requestStream = request.GetRequestStream();
        while (send.Length - bytesRead > 1)
            requestStream.Write(send, bytesRead++ , 1); //Edited
        requestStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader responseReader = new StreamReader(response.GetResponseStream());

        string responseStr = responseReader.ReadToEnd();

请帮助:)

1 个答案:

答案 0 :(得分:0)

查看代码

while (send.Length - bytesRead > 1)
        requestStream.Write(send, 0, bytesRead++);

这里你发送的是第一个字节,然后是前两个字节,然后是前三个字节,依此类推。请尝试以下方法(未经测试):

for(int i=0;i<send.Length;i++){
  requestStream.Write(send,i,1);
  bytesRead++;
}