帮助使用ASP.NET HttpWebRequest [英] Help with ASP.NET HttpWebRequest
问题描述
我开始学习ASP.NET和C#,以更加熟悉我想制作开放源代码插件的技术 BugTracker.Net 应用程序.我堆叠的是要添加新问题的应用程序,需要在insert_bug.aspx上获取HttpWebRequest
I began to learn ASP.NET and C#, to get more familiar with techniques I want to make plugin for open source BugTracker.Net application. Where I am stacked is that for adding new issue application need to get HttpWebRequest on insert_bug.aspx
我确实在处理像这样的代码上做了基本的事情:
I did make basic thing working on code like this:
string Url = "http://localhost:8090/insert_bug.aspx";
string post_data =
"&username=admin"
+ "&password=admin"
+ "&short_desc=Description"
+ "&comment=Comment"
+ "&projectid=1"
byte[] bytes = Encoding.UTF8.GetBytes(post_data);
HttpWebResponse res = null;
try
{
HttpWebRequest req = (HttpWebRequest) System.Net.WebRequest.Create(Url);
req.Credentials = CredentialCache.DefaultCredentials;
req.PreAuthenticate =
req.Method = "POST";
req.ContentType= "application/x-www-form-urlencoded";
req.ContentLength=bytes.Length;
Stream request_stream = req.GetRequestStream();
request_stream.Write(bytes,0,bytes.Length);
request_stream.Close();
res = (HttpWebResponse) req.GetResponse();
}
catch (Exception e)
{
Console.WriteLine("HttpWebRequest error url=" + Url);
Console.WriteLine(e);
}
我也想将CATEGORY插入到我的问题中,并阅读insert_bug.aspx上的代码,我资助了用于定义打开问题的类别的部分
I want to insert also CATEGORY into my issues and reading code on insert_bug.aspx i fund part for defining category for opening issue
if (Request["$CATEGORY$"] != null && Request["$CATEGORY$"] != "") { categoryid = Convert.ToInt32(Request["$CATEGORY$"]); }
问题:如何以及如何在请求中添加"$ CATEGORY $",以便添加的问题已定义类别.
Question: What and How I can add "$CATEGORY$" to my request so the issues I added have defined category.
推荐答案
如果我正确理解了您的问题,如果要添加类别,则需要查找类别ID并将其作为变量添加到POST数据中.例如,如果您想要的类别的ID为1(在BugTracker.NET中为错误"),则可以使用以下方式:
If I understand your question correctly, if you want to add a category, you need to look up the category ID and add it as a variable in your POST data. For example, if the category you want has its ID=1 ("bug" in BugTracker.NET), then you'd use this:
string post_data =
"&username=admin"
+ "&password=admin"
+ "&short_desc=Description"
+ "&comment=Comment"
+ "&projectid=1"
+ "&$CATEGORY$=1";
BTW, in simple HTTP client scenarios like this one you're better off using the WebClient
class. You can accomplish the same thing in many fewer lines of code.
这篇关于帮助使用ASP.NET HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!