I'm developing application in compact framework and need to send POST a Json to server
public SResponse SaveDoc(Document document)
{
var url = WorkSettings.URL + "savedoc/" + document.DocType + "/" + document.DocNumber;
string json = JsonConvert.SerializeObject(document);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes;
if (json != null)
{
postBytes = Encoding.UTF8.GetBytes(json);
}
else
{
postBytes = new byte[0];
}
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
return GetResponseData(response);
}
Exception Error
This request requires buffering of data for authentication or redirection to be successful.
I'm more familiar with HttpClient myself and have never used HttpWebRequest, but any errors I receive dealing with redirects can quickly be solved by making sure your URL is exactly correct.
var url = WorkSettings.URL + "savedoc/" + document.DocType + "/" + document.DocNumber
Make sure your URL above is exactly the URL you are trying to save to. Perhaps you've added an extra "/" in the URL where the error is occurring? Or perhaps you need to append ".php" or ".html" to the end of your url.
If you're not capable of debugging the code to step through what this exact URL is, you can also use Wireshark or Fiddler view if a redirect request is occurring.
Other questions to consider:
1) Do you want auto-redirects to occur if you send the wrong URL? Probably, but perhaps not. If not, it's worth pointing out that HttpWebRequest's AutoRedirect property is set true by default.
2) Do you want to handle this error specifically? Or perhaps the error is not caused by having a slightly incorrect URL. If so, I do not have much personal experience with handling this error. Perhaps this StackOverflow post has a very easy solution that might do the trick.