1

我正在尝试通过 wp-api 在 wordpress 上发帖,但出现错误:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (404) Not Found.

我无法理解,因为当我转到链接时插件工作正常:http ://example.com/wp-json/我正在按预期获取 json 数据,我没有收到任何错误告诉我插件工作正常。我的代码是这样的:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WordpressPost
{
class Program
{
    static void Main(string[] args)
    {
        var uri = "http://www.example.com/wp-json/";
        var username = "user";
        var password = "password";

        var webRequest = WebRequest.Create(uri);
        webRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
        webRequest.ContentType = "application/json";
        webRequest.Method = "POST";

        var streamWriter = new StreamWriter(webRequest.GetRequestStream());

        string json = "{\"title\":\"Hello World!\",\"content_raw\":\"Content\",\"excerpt_raw\":\"Excerpt\"}";

        streamWriter.Write(json);
        streamWriter.Close();

        var response = (HttpWebResponse)webRequest.GetResponse();
        var streamReader = new StreamReader(response.GetResponseStream());
        var result = streamReader.ReadToEnd();
        Console.WriteLine(result.ToString());

        Console.ReadLine();
    }
}
}

我已经安装了两个需要的插件 JSON 基本身份验证、WP REST API 并且还启用了漂亮的永久链接。我究竟做错了什么 ?

我是 C# 新手,这就是我选择基本身份验证登录的原因。

4

0 回答 0