Error 405 when trying to upload text to create a file

I am trying to create a file from a text string in a web server in a folder C:\inetpub\wwwroot\AlekaConsulting\AlekaConsulting\License using IIS 10.0.14393.0 on a Windows Server 2016 environment using the following Objective-C code on macOS Catalina, but no file is created and the returnString variable contains the error message
Code Block
405 - HTTP verb used to access this page is not allowed. The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.

The same error appears if I use PUT rather than POST as HTTPmethod.

No verbs are listed in the Request Filtering tab of IIS Manager, which means no verbs are blocked. webDav is not installed as a server feature.

Content can be successfully read from files in the specified folder.

Is the error connected with the URL being accessed via https?

Code Block
-(void) UploadFileContent: (NSString *) fileContent{
NSString *urlString = @"https://captionpro.com.au:444/AlekaConsulting/License";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:fileContent] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}

fg
This 405 error is coming back from the server, so it’s hard to give definitive answers about what’s causing it without looking on the server itself. For more on this, see Debugging HTTP Server-Side Errors.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Thanks Quinn. I checked the IIS server failed request log (which contains a lot of detail) and noticed that the request used anonymous authentication. It seems likely that the server would block anonymous users from anything but reading from a web page as a security measure, which is why I can read from a file in the folder but not write a new one in it. If this is the case, how do I add authentication to the request?
Some .Net code I use for this purpose is:

Code Block
using (WebClient client2 = new WebClient())
{
client2.UploadFile("https://captionpro.com.au:444/AlekaConsulting/License/Upload.aspx", sTempFile);
}
return false;

I notice a number of other people having problems uploading to a web site use a server-side script. Can I make use of Upload.aspx?
I have now got creation of a text file on a web server from a string working using the following code:

Code Block
- (void) UploadFileContent: (NSString *) fileContent{
        // URL for script file on server in folder where file will be created
        NSString *urlString = @"https://xxxx/xxxx/Upload.aspx"
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
         [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
         NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// UploadedFile.txt is name of file to be created
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"UploadedFile.txt\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:fileContent] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // set the body of the post to the request
        [request setHTTPBody:body];
        // connect to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}

The Upload.aspx file is

Code Block
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
// FolderPath = path to folder containing file to be created eg C:\inetpub\wwwroot\***\***\***\
string sFilePath = @"FolderPath" + file.FileName;
if(System.IO.File.Exists(sFilePath)) System.IO.File.Delete(sFilePath);
file.SaveAs(sFilePath);
}
}
</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>


Error 405 when trying to upload text to create a file
 
 
Q