我正在尝试连接visual studio和salesforce。在visual studio中,我必须构建url以请求令牌。使用stringbuilder
函数构建为Append()
对象的url。我在其余控制台中使用url时获取令牌。但是,visual studio中生成的url会在密码和安全令牌之间插入一个额外的字符。只有当我选择ANSI编码时才能看到额外的字符。
StringBuilder body = new StringBuilder();
//body.Append("code=" + code + "&");
body.Append("grant_type=password&");
body.Append("client_id=" +clientID +"&");
body.Append("client_secret="+clientSecret+"&");
body.Append("username="+username+"&");
body.Append("password=" + pass);
body.Append(security_token);
string result = HttpPost(URI, body.ToString());
/ * ** * ** * ** * ** * ** * **** HttpPost的代码() ** * ** * **
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
// Add parameters to post
byte[] data = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = data.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();
// Do the post and get the response.
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
运行上面的代码会在password
和security_token
之间插入一个额外的字符,从而产生System.Net.WebException
。获得响应时会发生异常。谁能告诉我这个的原因?
答案 0 :(得分:1)
您尚未提供引发错误的代码。既然你提到字符串是一个URL,所以我猜测浏览器会添加额外的字符。所以我建议使用UrlEncode对您的URL进行编码。并使用UrlDecode读取原始网址。
HttpServerUtility.UrlEncode(body.ToString());
要了解有关UrlEncode的更多信息,请访问here