401与Restful Services交互时出错

时间:2017-04-25 19:54:28

标签: c# asp.net rest http

我对HTTP请求和RESTful API一般都很陌生。我目前正通过ASP.Net应用程序与服务进行交互。我使用的服务需要加密授权才能访问他们的POST,PUT和DELETE方法。

我相当确定我已根据我提供的手册正确获得了授权。 H.我目前正在测试Delete方法。当我删除网页上的条目时,我遇到了#34; 401 Unauthorized错误"。 Visual Studio告诉我401错误的状态是" ProtocolError"。我的授权是否有问题?我不包括我需要的标题吗?我需要编辑我的web.config文件吗?我在本地计算机上运行该站点。非常感谢任何帮助或建议。谢谢。

以下是我用来获取授权的方法:

public static string HMACSHA1(string key, string dataToSign)
  {
     Byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(key);
     HMACSHA1 hmac = new HMACSHA1(secretBytes);

     Byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(dataToSign);
     Byte[] calcHash = hmac.ComputeHash(dataBytes);
     String calcHashString = Convert.ToBase64String(calcHash);
     return calcHashString;

  }

  public string get_auth(string HTTP_METHOD, string uri)
  {
     string auth = "EWS" + " " + Access_ID() + ":" + get_signature(HTTP_METHOD, uri);
     return auth;
  }
  public string get_signature(string HTTP_METHOD, string resource)
  {
     string userpass = "userpass";
     string StringToSign = get_string_to_sign(HTTP_METHOD, get_resource());
     string signature = string.Empty;
     return HMACSHA1(userpass, StringToSign);
  }
  public string Access_ID()
  {
     string Access_ID = string.Empty;
     string username = "username";
     string userName = Convert.ToBase64String(Encoding.UTF8.GetBytes(username));
     return userName; 

  }
  public string get_string_to_sign(string HTTP_METHOD,string resource)
  {

     string string_to_sign = string.Empty;
     string_to_sign = HTTP_METHOD + "\n" + DateTime.Now.ToString("r")  +"\n"+  resource;
     return string_to_sign; 
  }

这是我的删除方法:

public void Delete(string destination_url)
  {

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destination_url);
     request.Method = "DELETE";
     request.Headers["x-zws-date"] = DateTime.Now.ToString("r");
     string auth = get_auth(request.Method, destination_url);
     request.Headers.Add("Authorization", auth);
     request.KeepAlive = true;
     request.ProtocolVersion = HttpVersion.Version10;
     request.AllowWriteStreamBuffering = true;

     HttpWebResponse response;

     response = (HttpWebResponse)request.GetResponse();
     try
     {
        if (response.StatusCode == HttpStatusCode.OK)
        {
           Stream responseStream = response.GetResponseStream();
           string responseStr = new StreamReader(responseStream).ReadToEnd();
        }
     }
     catch (WebException ex)
     {

        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
           HttpWebResponse responsed = ex.Response as HttpWebResponse;

           var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
           Response.Write("An error occurred, status code: " + statusCode);
        }
     }
     catch (Exception ex)
     {
        Response.Write(ex.Message.ToString());
        Response.Write(ex.StackTrace.ToString());
     }
  }

从文档中删除示例:

HTTP method 
DELETE 
Authorization: Yes 
Variable: None 
Status code 
200 OK Successful 
400 Bad Request Data does not match with ID or this type does not exist. 
401 Unauthorized User name or password is wrong or the authority of a user 
to access to this data processing has not been obtained. 
404 Not Found Data record not found. 
405 Method Not Allowed The data record could not be deleted since there is 
physical sample corresponding to this record. 

Example 
http://localhost//webservicename/Tool/4444 
DELETE http://localhost/webservicename/Tool/4444 HTTP/1.1 Authorization: ZWS 
dXNlcg==:dXNlcg== User-Agent: Jakarta Commons-HttpClient/3.1 Host: localhost 
Date: Thu, 10 Jun 2010 10:42:52 GMT 
Reply 
HTTP/1.1 200 OK Content-Length: 0 Server: Microsoft-HTTPAPI/1.0 Date: Thu, 
10 Jun 2010 10:42:52 GMT 

0 个答案:

没有答案