小编Ars*_*ray的帖子

如何使用C#将JSON发布到服务器?

这是我正在使用的代码:

// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";


// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(json);

// this is important - make sure you specify type this way
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
request.CookieContainer = Cookies;
request.UserAgent = currentUserAgent;
Stream requestStream = request.GetRequestStream();

// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

// grab te response and print it out …
Run Code Online (Sandbox Code Playgroud)

c# post json httpwebrequest

246
推荐指数
10
解决办法
56万
查看次数

在WPF中使用Hyperlink的示例

我已经看到了一些建议,你可以通过Hyperlink控件添加到WPF应用程序的超链接.

这是我在我的代码中尝试使用它的方式:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="BookmarkWizV2.InfoPanels.Windows.UrlProperties"
        Title="UrlProperties" Height="754" Width="576">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Grid>
            <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="2">
                <StackPanel >
                    <DockPanel LastChildFill="True" Margin="0,5">
                        <TextBlock Text="Url:" Margin="5" 
                            DockPanel.Dock="Left" VerticalAlignment="Center"/>
                        <TextBox Width="Auto">
                            <Hyperlink NavigateUri="http://www.google.co.in">
                                    Click here
                            </Hyperlink>   
                        </TextBox>                      
                    </DockPanel >
                </StackPanel>
            </ScrollViewer>        
        </Grid>
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,7,2,7" Grid.Row="1" >
            <Button Margin="0,0,10,0">
                <TextBlock Text="Accept" Margin="15,3" />
            </Button>
            <Button Margin="0,0,10,0">
                <TextBlock Text="Cancel" Margin="15,3" />
            </Button>
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

属性"文本"不支持"超链接"类型的值.

我究竟做错了什么?

c# wpf xaml hyperlink

149
推荐指数
7
解决办法
18万
查看次数

财产变化的断点

Firebug for Firefox有一个很好的功能,名为"Break on property change",我可以在其中标记任何对象的任何属性,它将在更改之前停止JavaScript执行.

我正在尝试在Google Chrome中实现相同功能,但我无法在Chrome调试器中找到该功能.如何在Google Chrome中执行此操作?

javascript debugging firefox firebug google-chrome

139
推荐指数
5
解决办法
5万
查看次数

如何获得等待的Thread.Sleep?

我正在编写一个基于await/sleep范例的网络绑定应用程序.

有时会发生连接错误,根据我的经验,等待一段时间再重试操作是值得的.

问题是,如果我在await/async中使用Thread.Sleep或类似的阻塞操作,它会阻止调用程序线程中的所有活动.

我应该用什么来替换Thread.Sleep(10000)以获得相同的效果

await Thread.SleepAsync(10000)
Run Code Online (Sandbox Code Playgroud)

UPDATE

我更喜欢一个没有创建任何额外线程的答案

.net c# multithreading async-await

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

如何在程序启动时请求管理员权限?

我需要我的软件能够在Windows Vista上以管理员身份运行(如果有人在没有管理权限的情况下运行它,它将崩溃).

在启动其他软件时,我看到系统提示"此软件将以管理员身份运行.您要继续吗?" 当应用程序尝试获取管理权限时.

在Windows Vista上运行c#app时如何请求管理权限?

c# windows windows-vista windows-7

72
推荐指数
2
解决办法
7万
查看次数

从WPF DataGrid复制粘贴数据时,OpenClipboard失败

我有一个使用datagrid的WPF应用程序.该应用程序运行正常,直到我安装Visual Studio 2012和Blend + SketchFlow预览.现在,当我尝试使用Ctrl+ C(在任何应用程序中)将数据从网格复制到剪贴板时,我得到以下异常:

System.Runtime.InteropServices.COMException (0x800401D0): OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
   at System.Windows.Clipboard.Flush()
   at System.Windows.Clipboard.CriticalSetDataObject(Object data, Boolean copy)
   at System.Windows.Controls.DataGrid.OnExecutedCopy(ExecutedRoutedEventArgs args)
   at System.Windows.Controls.DataGrid.OnExecutedCopy(Object target, ExecutedRoutedEventArgs args)
   at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
   at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) …
Run Code Online (Sandbox Code Playgroud)

c# wpf clipboard datagrid copy-paste

68
推荐指数
4
解决办法
4万
查看次数

使用反射检查java中的字段是否为final

我正在写一个课程,在某些时候,必须Field从该课程的另一个项目中分配所有课程.

我通过反思做到了:

for (Field f:pg.getClass().getDeclaredFields()) {
    f.set(this, f.get(pg));
}
Run Code Online (Sandbox Code Playgroud)

问题是,这个类包含一个Field,即final.我可以跳过它的名字,但对我来说似乎并不优雅.

检查a Field是否final在使用反射的java中的最佳方法是什么?

java

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

在多线程应用程序中使用Random的正确方法

好.以下是我所知道的不起作用:

int Rand()
{
    //will return the same number over and over again
    return new Random().Next();
}

static Random rnd=new Random();

int Rand()
{
    //if used like this from multiple threads, rnd will dissintegrate 
    //over time and always return 0
    return rnd.Next();
}
Run Code Online (Sandbox Code Playgroud)

这将正常工作,但如果由多个线程使用,CPU使用率上升,我不想要,我认为没有必要:

int Rand()
{
    lock(rnd)
    {
        return rnd.Next();
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,c#是否有一个线程安全的Random类,或者更好的方法来使用它?

c# random

43
推荐指数
3
解决办法
1万
查看次数

TCP连接中的FIN与RST

我理解这一点,有两种方法可以关闭TCP连接:

  • 发送FIN标志
  • 发送RST标志

RST导致立即连接终止,而在FIN中您会得到确认.

我是否理解这一点,两者之间是否存在其他区别?这两个标志可以一起使用吗?

networking tcp

35
推荐指数
3
解决办法
7万
查看次数

开始使用protobuf-net

我正在尝试使用protobuf.net.

我从这里下载了最新版本,实现了一个[ProtoContract]类,现在我正在尝试使用文档中ProtoBuf.Serializer.Serialize描述的序列化它.问题是,没有这样的方法.

我该如何写入[ProtoContract]磁盘?

更新 显然,我有错误的版本或类似的东西,但这是我Serialize班级的样子:

我看到protobuf.net Serializer类

c# protobuf-net

32
推荐指数
2
解决办法
4万
查看次数