小编Sia*_*lis的帖子

将凭证传递给WebProxy?

我有一个自定义HTTP类用于我正在使用的服务.最终它将以方法的形式包含特定于服务的请求.我需要做的是设置用户提供的代理的凭据,例如,如果用户有代理列表.

以下是我的代码.我已经评论了我需要设置凭据的部分.我查看了MSDN上的iCredentials类,但我看不到如何从字符串中设置它们.

class RequestClass
{
    private CookieContainer cookieJar;
    private WebProxy proxy = null;

    public RequestClass()
    {
        this.cookieJar = new CookieContainer();
    }

    public RequestClass(String proxyURL, int port)
    {
        this.proxy = new WebProxy(proxyURL, port);
    }

    public RequestClass(String proxyURL, int port, String username, String password)
    {
        this.proxy = new WebProxy(proxyURL, port);
        // Need to set them here
    }

    // HTTP Get Request
    public HttpWebResponse getRequest(String url, NameValueCollection headers)
    {
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
        getRequest.Method = "GET";
        getRequest.CookieContainer = cookieJar;

        foreach (String key in …
Run Code Online (Sandbox Code Playgroud)

c#

16
推荐指数
1
解决办法
2万
查看次数

活动和代表.什么设计模式?

在学习事件和代表的同时,我不禁会想到观察者的设计模式.

对于C#和设计模式,我都是新手.当使用事件和委托让我们说,从子表单中触发事件并被父表单接收时,观察者设计模式是什么?

什么其他设计模式是事件和代表使用?

我有兴趣了解.NET中的其他"事物"(对于我缺乏术语而言)是基于常见的设计模式,例如来自四人帮的设计模式.

我认为,如果您可以将它与模式相关联,那么理解代码示例和解释这些主题会更容易.无论如何,个人而言.

谢谢.

c# design-patterns

5
推荐指数
1
解决办法
1396
查看次数

随机种子递归函数.我该怎么做?

我有一个递归函数,在函数中选择了一个数组中的随机元素,但无论我做什么,我都会得到相同的种子.

static Random rand = new Random();
public String spintaxParser(String s)
    {
        if (s.Contains('{'))
        {
            int closingBracePosition = s.IndexOf('}');
            int openingBracePosition = closingBracePosition;

            while (!s[openingBracePosition].Equals('{'))
                openingBracePosition--;

            String spintaxBlock = s.Substring(openingBracePosition, closingBracePosition - openingBracePosition + 1);

            String[] items = spintaxBlock.Substring(1, spintaxBlock.Length - 2).Split('|');


            s = s.Replace(spintaxBlock, items[rand.Next(items.Length)]);

            return spintaxParser(s);
        }
        else
        {
            return s;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在递归函数中处理Random的最佳方法是什么?

c#

5
推荐指数
1
解决办法
609
查看次数

Spintax C#...我该如何处理?

Spintax允许您旋转各种单词和句子,例如:

{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.
Run Code Online (Sandbox Code Playgroud)

大括号之间的文本将随机选择以形成不同的句子.

我自己可能想出一个解决方案,但我遇到的问题是嵌套.有时嵌套可能非常深.处理嵌套的可能解决方案是什么?

我无法收集所需的逻辑.

c# spintax

4
推荐指数
1
解决办法
2689
查看次数

标签 统计

c# ×4

design-patterns ×1

spintax ×1