The remote server returned an error: (403) Forbidden. error?
below is my code..I get an error -"The remote server returned an error: (403) Forbidden."..:
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.</s开发者_JAVA技巧ummary>
<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();
please help :)
Look at the code
while (send.Length - bytesRead > 1)
requestStream.Write(send, 0, bytesRead++);
Here you're sending the first byte, then the first two, then the first three, and so on. Try the following instead (not tested):
for(int i=0;i<send.Length;i++){
requestStream.Write(send,i,1);
bytesRead++;
}
精彩评论