2017-03-07 7 views
3

私はいくつかの既存のコードがあり、突然終了しました。私は理由を理解することはできませんC#REST APIコール - コードではなく郵便番号での作業

...ここ

は私のコードです:

public static string RequestToken(string u, string pw) 
    { 
     string result = string.Empty; 

     string strUrl = "https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=" + u + "&password=" + pw; 
     HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest; 
     Debug.Print(strUrl); 
     tokenRequest.Method = "POST"; 
     try 
     { 
      using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse) 
      { 
       if (tokenResponse.StatusCode != HttpStatusCode.OK) 
        throw new Exception(String.Format(
         "Server error (HTTP {0}: {1}).", 
         tokenResponse.StatusCode, 
         tokenResponse.StatusDescription)); 
       DataContractJsonSerializer jsonSerializer2 = 
        new DataContractJsonSerializer(typeof(ResponseAuthentication)); 
       object objTokenResponse = jsonSerializer2.ReadObject(tokenResponse.GetResponseStream()); 
       ResponseAuthentication jsonResponseAuthentication = objTokenResponse as ResponseAuthentication; 
       result = jsonResponseAuthentication.strAccessToken; 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.Print(ex.InnerException.ToString()); 
     } 
     return result; 
    } 

これはきれいに働いていた前に、私は今500 Internal Server Errorどこを取得しています。

私がPostmanを使用してデバッグしようとすると、URLを直接渡して正常に動作します。私はコードに停止を入れて、コード内で失敗したのと全く同じURLを使用しても、Postmanでは動作しますが、C#では動作しません。郵便配達のうち

、私は

{ 
    "access_token": "XXXXXX", 
    "instance_url": "https://xxx.cloudforce.com", 
    "id": "https://login.salesforce.com/id/XXXXXX", 
    "token_type": "Bearer", 
    "issued_at": "XXXXXX", 
    "signature": "XXXXXX" 
} 

は、私が代わりにPOSTGET要求を試してみましたが、明確にするために...整然とした取得し、私は(ポストマンに)次の応答が返されます。

{ 
    "error": "invalid_request", 
    "error_description": "must use HTTP POST" 
} 

ここにアイデアはありますか?

+0

確かに、メソッドの型は 'POST'にする必要がありますか? – levent

+0

@levent - はい、それは私が郵便配達でも使っているものです... GETは動作しません。 – gotmike

+0

郵便配達員の成功事例でrequest.contentTypeとは何ですか? – levent

答えて

7

それで、Salesforceが.NET 4.5のデフォルトであるTLS 1.0のサポートを終了したという回答が終わりました。

このリンクでは、これは2017年の7月に起こるはずだと述べていますが、どういうわけか私たちのインスタンスを早く打ちました。 https://help.salesforce.com/articleView?id=000221207&type=1

私たちは、後でそれが4.5で.NET 4.6をで動作しますが、しないことを確認...

は、TLS 1.2を使用する.NET 4.6のデフォルトをオンにします。

これは非常に簡単な修正であり、これは長い時間がかかりました。

は、コードのこの1行を追加し、それがすぐに働いた:同じ問題で他の誰かを助け

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

願っています!

このような単純な1行の解決策が見つかるには、少しイライラします。

+0

私を保存していただきありがとうございます。私は複数回upvoteすることができれば願っています。 – Saurabh

+0

感謝します!!!!!!! – Gaspa79

+0

この行はどこに追加しましたか? –