Hi All,
I have used the sample code provided on the link
code.msdn.microsoft.com/.../Discussions
to create a plugin firing synchronously on creation of account record to create a folder in the SharePoint online.
I am getting error message as below:
The remote server returned an error: (411) Length Required. By debugging the code I can see that it is failing in the method
public static byte[] SendHttpRequest(Uri uri, String method, byte[] requestContent = null, string contentType = null, HttpWebRequest clientHandler = null, Dictionary<string, string> headers = null)
{
HttpWebRequest request = clientHandler == null ? (HttpWebRequest)HttpWebRequest.Create(uri): clientHandler;
byte[] responseStream;
request.Method = method;
request.Accept = contentType;
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // This must be here as you will receive 403 otherwise
request.AllowAutoRedirect = false; // This is key, otherwise it will redirect to failed login SP page
// append additional headers to the request
if (headers != null)
{
foreach (var header in headers)
{
if (request.Headers.AllKeys.Contains(header.Key))
{
request.Headers.Remove(header.Key);
}
request.Headers.Add(header.Key, header.Value);
}
}
if (requestContent != null && (method == "POST" || method == "PUT" || method == "DELETE"))
{
if (!string.IsNullOrEmpty(contentType))
{
request.ContentType = contentType; // if the request has a body set the MIME type
}
request.ContentLength = requestContent.Length > 0 ? requestContent.Length : 0;
using (Stream s = request.GetRequestStream())
{
s.Write(requestContent, 0, requestContent.Length);
s.Close();
}
}
// Not using Using here as you may still like to access the reponse outside of this method
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
responseStream = Encoding.UTF8.GetBytes(sr.ReadToEnd());
return responseStream;
}
The call to this method is from the method as below:
private Uri GetAdfsAuthUrl()
{
Uri corpAdfsProxyUrl = null;
Uri msoHrdUri = new Uri(msoHrdUrl);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(msoHrdUri);
// make a post request with the user's login name to
// MSO HRD (Home Realm Discovery) service so it can find
// out the url of the federation service (corporate ADFS)
// responsible for authenticating the user
byte[] response = HttpHelper.SendHttpRequest(
new Uri(msoHrdUrl),
"POST",
Encoding.UTF8.GetBytes(String.Format("handler=1&login={0}", this.username)), // pass in the login name in the body of the form
"application/x-www-form-urlencoded",
request);
STSInfo info = Deserialize<STSInfo>(response);
if (info != null && !String.IsNullOrEmpty(info.AuthURL))
{
corpAdfsProxyUrl = new Uri(info.AuthURL);
}
return corpAdfsProxyUrl;
}
The content length is definitely been mentioned and is not zero. Not sure why I am getting this error message.
Any help in this regards is much appreciated.
Many Thanks
*This post is locked for comments