【问题标题】:cURL with user authentication in C#在 C# 中具有用户身份验证的 cURL
【发布时间】:2011-07-06 09:08:15
【问题描述】:

我想在 c# 中执行以下 cURL 请求:

curl -u admin:geoserver -v -XPOST -H 'Content-type: text/xml' \
   -d '<workspace><name>acme</name></workspace>' \
   http://localhost:8080/geoserver/rest/workspaces

我尝试过使用 WebRequest:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("admin", "geoserver");

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

但我收到一个错误:(400) 错误请求。

如果我更改请求凭据并在标头中添加身份验证:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
string authInfo = "admin:geoserver";
request.Headers["Authorization"] = "Basic " + authInfo;

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

然后我得到:(401)未经授权。

我的问题是:我应该使用另一个 C# 类,如 WebClient 或 HttpWebRequest,还是必须使用 .NET 的 curl 绑定?

所有 cmets 或指导将不胜感激。

【问题讨论】:

    标签: c# .net authentication curl geoserver


    【解决方案1】:

    HTTP Basic 身份验证要求 "Basic " 之后的所有内容都进行 Base64 编码,因此请尝试

    request.Headers["Authorization"] = "Basic " + 
        Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
    

    【讨论】:

    • 我已尝试将 authInfo 转换为 BAse64String,但随后出现“(400) 错误请求错误”。
    • @taudorf: 嗯...下载 Fiddler 看看两个 POST 请求有什么区别。
    【解决方案2】:

    我的问题的解决方案是更改 ContentType 属性。如果我将 ContentType 更改为

    request.ContentType = "text/xml";
    

    如果我在最后一个示例中也将 authInfo 转换为 Base64String(如 Anton Gogolev 建议的那样),则该请求适用于这两种情况。

    【讨论】:

      【解决方案3】:

      使用:

      request.ContentType = "application/xml";
      
      request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);
      

      也可以。第二个设置身份验证信息。

      【讨论】:

        【解决方案4】:

        或者,如果你想使用 HttpClient:

         var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("admin:geoserver")));
                try
                {
                    var client = new HttpClient()
                    {
                        DefaultRequestHeaders = { Authorization = authValue }
                    };
        
                    string url = "http://{BASE_URL}";
                    client.BaseAddress = new Uri(url);
        
                    var content = new StringContent("<workspace><name>TestTestTest</name></workspace>",
                         Encoding.UTF8, "text/xml");
                    var response = await client.PostAsync($"/{PATH_TO_API}/", content);
                    response.EnsureSuccessStatusCode();
                    var stringResponse = await response.Content.ReadAsStringAsync();
                }
                catch (HttpRequestException ex)
                {
                    Console.WriteLine(ex.Message);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-24
          • 1970-01-01
          • 1970-01-01
          • 2020-09-28
          • 2016-10-22
          相关资源
          最近更新 更多