C#和REST.API问题 [英] C# and REST.API question
问题描述
嘿同胞Cpians,
我在使用REST.API与服务器连接时出现问题。下面发布的代码是EXAMPLECODE。
一旦我到达client.DownloadData(...)它就会失败握手产生错误连接是发送时出现意外错误......。
内部异常是握手时出错,意外的包格式。
I不知道问题是什么,因为这是我第一次尝试使用REST和C#。
BTW URI字符串:https:// servername:8080 / cb / rest / user / myuser / items / page / 1?role =分配给& status =未解决& pagesize = 50& onlyDirect = False;
另外,当我把URI放入firefox我收到数据,但程序崩溃。所以我想这是一个C#问题?
谢谢:)
编辑:
我尝试连接时不使用https://而只使用http://,这实际上有效,但我没有得到任何数据(正如我预期的那样)。这是某种SSL问题吗?
最后:
感谢Sacha提供的信息,我会记住这一点未来,现在我只是在应用程序中使用http://再次尝试。如果我用downloadString替换downloadData,它的工作原理。现在我必须让DataDownload正确并且它会没问题。
我尝试了什么:
静态 void Main ( string [] args)
{
ProgramArguments arguments = parseArguments(args);
if (arguments.Debug)
{
printOutArguments(arguments);
}
int pageNum = 1 ;
bool ready = false ;
// init web client
WebClient client = getWebClient(参数);
// init input serializer
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof (ResultObject));
列表< WorkItem> workItems = new 列表< WorkItem>();
// 在列出所有问题时使用分页获取所有问题
while (!ready)
{
// 创建数据获取URL onlyDirect
StringBuilder urlBuilder = new StringBuilder( 500 跨度>);
urlBuilder.Append(arguments.Endpoint).Append( / cb / rest / user /跨度>)。追加(arguments.UserName)。
Append( / items / page /)。追加(pageNum).Append( ?)。追加( role =分配给)。
Append( & status = Unresolved)。追加( & pagesize =)。Append(arguments.PageSize).Append( & onlyDirect =)。Append(arguments.OnlyDirect);
尝试
{
// 从创建的URL中获取数据
使用(MemoryStream input = new MemoryStream(client.DownloadData(urlBuilder.ToString())))
{
ResultObject resultObject =(ResultObject)serializer.ReadObject(input);
// 将提取的问题添加到结果列表
if (resultObject.items!= null )
{
foreach (项目 in resultObject.items)
{
IItemConverter converter = getItemConverter(arguments);
workItems.Add(converter.Convert(item));
}
}
// 检查是所有项目已下载
ready =(pageNum * arguments.PageSize)> resultObject.total;
// 转到下一页
pageNum ++;
}
}
catch (例外e)
{
if (arguments.Debug)
{
Console.WriteLine( 错误:{0},e.ToString());
}
ready = true ;
}
}
if (arguments.Debug)
{
Console.WriteLine(workItems.Count + 从codeBeamer获取的问题!);
}
StreamWriter writer = null ;
尝试
{
writer = getOutputWriter(arguments);
// 通过提供商生成输出
OutputProviderFactory.GetProvider(arguments).GenerateOutput (workItems,作家);
}
最后
{
如果(作家!= null )
{
writer.Close();
}
}
}
看看这里我展示了不同的方式来使用.NET进行REST调用,评论中还有另一种方式:
JSON API [ ^ ]
Hey fellow Cpians,
i have a problem with a connection to a Server with the REST.API. The code posted below is EXAMPLECODE.
As soon as i reach the point of client.DownloadData(...) it fails the Handshake producing the error "The connection was closed. Unexpected error while sending...".
The inner Exception is "Error on handshake, unexpected package format".
I don'T have any clue what the problem is since this is my first try with REST and C#.
BTW the URI String : "https://servername:8080/cb/rest/user/myuser/items/page/1?role=assigned to&status=Unresolved&pagesize=50&onlyDirect=False";
Additionally, when i put the URI into the firefox i recieve data, but the program crashes. So i guess this is a C# problem?
Thanks :)
EDIT:
I tried connecting without https:// and instead using only http://, this actually works but i don'T get any data back (as i expected). So is this some kind of SSL issue?
Finally:
Thanks Sacha for your info, i'll keep that in mind for the future, for now i just tried again with http:// in the app. If i replace downloadData with downloadString it works. Now i have to get the "DataDownload" correct and it'll be fine.
What I have tried:
static void Main(string[] args)
{
ProgramArguments arguments = parseArguments(args);
if (arguments.Debug)
{
printOutArguments(arguments);
}
int pageNum = 1;
bool ready = false;
// init web client
WebClient client = getWebClient(arguments);
// init input serializer
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResultObject));
List<WorkItem> workItems = new List<WorkItem>();
// fetch all isses using paging while all issues is listed
while (!ready)
{
// create data fetch URL onlyDirect
StringBuilder urlBuilder = new StringBuilder(500);
urlBuilder.Append(arguments.Endpoint).Append("/cb/rest/user/").Append(arguments.UserName).
Append("/items/page/").Append(pageNum).Append("?").Append("role=assigned to").
Append("&status=Unresolved").Append("&pagesize=").Append(arguments.PageSize).Append("&onlyDirect=").Append(arguments.OnlyDirect);
try
{
// fetch the data from the created URL
using (MemoryStream input = new MemoryStream(client.DownloadData(urlBuilder.ToString())))
{
ResultObject resultObject = (ResultObject)serializer.ReadObject(input);
// add fetched issues to result list
if (resultObject.items != null)
{
foreach (Item item in resultObject.items)
{
IItemConverter converter = getItemConverter(arguments);
workItems.Add(converter.Convert(item));
}
}
// check is all of the items is downloaded
ready = (pageNum * arguments.PageSize) > resultObject.total;
// go to next page
pageNum++;
}
}
catch (Exception e)
{
if (arguments.Debug)
{
Console.WriteLine("Error: {0}", e.ToString());
}
ready = true;
}
}
if (arguments.Debug)
{
Console.WriteLine(workItems.Count + " issue fetched from codeBeamer!");
}
StreamWriter writer = null;
try
{
writer = getOutputWriter(arguments);
// generate output via providers
OutputProviderFactory.GetProvider(arguments).GenerateOutput(workItems, writer);
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
have a look here I show loads of different ways to use .NET to do REST calls, there is another way shown in the comments:
JSON API[^]
这篇关于C#和REST.API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!