连接到网络的API使用C# [英] connecting to web api using c#
本文介绍了连接到网络的API使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用下面的code到silverpop虽然我试图在获得的400状态code到autheniicate。因为我不知道还有什么尝试任何建议?我可以看到通话外出使用招,但我已经用尽了想法。 非常感谢
服务器返回以下错误消息:
code段
VAR httpWReq =(HttpWebRequest的)WebRequest.Create(https://api5.silverpop.com/oauth/token);
VAR POSTDATA = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId,config.ClientSecret,config.RefreshToken);
//也可以尝试这个字符串,但我得到了同样的答复
// VAR POSTDATA = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId,config.ClientSecret,config.RefreshToken);
VAR编码=新ASCIIEncoding();
VAR数据= encoding.GetBytes(POSTDATA);
httpWReq.Method =POST;
httpWReq.ContentType =X WWW的形式,urlen codeD;
httpWReq.ContentLength = data.Length;
使用(VAR流= httpWReq.GetRequestStream())
{
stream.Write(数据,0,data.Length);
}
变种响应=(HttpWebResponse)httpWReq.GetResponse();
VAR的结果=新的StreamReader(response.GetResponseStream())ReadToEnd()。
从服务器响应
远程服务器返回错误:(400)错误的请求。
解决方案
这似乎工作:
VAR httpWReq =(HttpWebRequest的)WebRequest.Create(https://api5.silverpop.com/oauth/token+ string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}",客户端ID,clientSecret,refreshToken));
VAR POSTDATA =;
VAR编码=新ASCIIEncoding();
VAR数据= encoding.GetBytes(POSTDATA);
httpWReq.Method =POST;
httpWReq.ContentType =X WWW的形式,urlen codeD;
httpWReq.ContentLength = data.Length;
使用(VAR流= httpWReq.GetRequestStream())
{
stream.Write(数据,0,data.Length);
}
变种响应=(HttpWebResponse)httpWReq.GetResponse();
VAR的结果=新的StreamReader(response.GetResponseStream())ReadToEnd()。
I'm trying to autheniicate using the code below to silverpop although I'm getting a status code of 400 when attempting. Any suggestions as I'm not sure what else to try?! I can see the call going out using Fiddler but I've ran out of ideas. Many thanks
The server is returning the following error message:
Code snippet
var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token");
var postData = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
// Also try this string but I get the same response
//var postData = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (var stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)httpWReq.GetResponse();
var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Response from server
The remote server returned an error: (400) Bad Request.
解决方案
This seems to work:
var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token" + string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", clientId, clientSecret, refreshToken));
var postData = "";
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (var stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)httpWReq.GetResponse();
var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
这篇关于连接到网络的API使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文