我正在编写一些调用Web服务的代码,读取响应并对其执行某些操作.我的代码名义上看起来像这样:
string body = CreateHttpBody(regularExpression, strategy);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "text/plain; charset=utf-8";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(body), 0, body.Length);
requestStream.Flush();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
byte[] data = new byte[response.ContentLength];
using (Stream stream = response.GetResponseStream())
{
int bytesRead = 0;
while (bytesRead < data.Length)
{
bytesRead += stream.Read(data, bytesRead, data.Length - bytesRead);
}
}
return ExtractResponse(Encoding.UTF8.GetString(data));
}
Run Code Online (Sandbox Code Playgroud)
我实际上进行任何自定义操作的唯一部分是在ExtractResponse和CreateHttpBody方法中.然而,仅仅对这些方法进行单元测试感觉是错误的,并且希望其余的代码能够正确地组合在一起.有什么方法可以拦截HTTP请求并提供模拟数据吗?
编辑此信息现已过期.使用System.Net.Http.HttpClient库构建此类代码要容易得多.
我一直想知道的关于rails的东西是能够将额外的数据传递给rails中的find_or_create方法.例如,我不能做以下事情
User.find_or_create_by_name('ceilingfish', :email => 'an_email@a.domain', :legs => true, :face => false)
Run Code Online (Sandbox Code Playgroud)
我可以
u = User.find_or_create_by_name('ceilingfish')
u.update_attributes(:email => 'an_email@a.domain', :legs => true, :face => false)
Run Code Online (Sandbox Code Playgroud)
但那更加丑陋,还需要三个查询.我想我能做到
User.find_or_create_by_name_and_email_and_face_and_legs('ceilingfish','an_email@a.domain',true, false)
Run Code Online (Sandbox Code Playgroud)
但那种意味着我知道什么样的价值观email,legs和face是.有谁知道这是否有一种非常优雅的方式?
我正在使用git-tfs,我想知道是否可以使用合并两个TFS分支git-tfs.我有两个分支$/MyCompany/Dev&$/MyCompany/Release-3.3.Release-3.3源自Dev分支.两者都使用git tfs检出不同的git存储库.
我想将更改重新合并到Dev分支中.这可以用git-tfs或者我是否必须尝试使用TFS工具来实现?
我为自定义项目设置创建了一个Maven原型,它运行得非常好,但我想知道是否可以传递额外的参数以便我可以做一些更有趣的模板.我试过类似的东西
mvn archetype:generate -DarchetypeCatalog=local -DdbHost=localhost
Run Code Online (Sandbox Code Playgroud)
并把
...
<option name="db.host.config.option" value="${dbHost}" />
...
Run Code Online (Sandbox Code Playgroud)
在我的模板中,但似乎不起作用.有没有办法用原型做到这一点?
我正在尝试管理我的节点包依赖项.我希望能够通过运行命令来安装所有必需的依赖项,从我所读到的,实现此目的的一种方法是使用package.json文件并运行npm install.所以我的JSON文件看起来像这样:
{
"name": "Name-Of-The-Thing",
"description": "The Thing's Name",
"author": "The Dude <the.dude@dudethinking.com>",
"dependencies": {
"mocha":">= 1.12.0",
"mocha-phantomjs":">= 3.1.0",
"chai":">= 1.7.2",
"phantomjs":">= 1.9.1"
}
}
Run Code Online (Sandbox Code Playgroud)
但是npm install报告以下错误:
npm ERR! Failed to parse json
npm ERR! Unexpected token ?
npm ERR! File: C:\Path\To\The\Thing\package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the …Run Code Online (Sandbox Code Playgroud) 我正在尝试对包含多字节字符的字符串执行子字符串,但我没有得到我期望的结果.我试图像?test那样对字符串进行子串.第一个字符是一个4字节字符,因此调用ToCharArray此字符串将返回:
因此,当我调用.Substring(1)此字符串时,它返回一个无效的字符串,该字符串以第一个字符的第三个和第四个字节开头,而不是"test".有没有办法获得.Substring和其他字符串操作将该字符视为一个单元?
我已经构建了我的第一个宝石,但我似乎无法正确安装它.我可以发出命令
sudo gem install ceilingfish-toto
Run Code Online (Sandbox Code Playgroud)
哪个产生输出
Successfully installed ceilingfish-toto-0.3.6
1 gem installed
Run Code Online (Sandbox Code Playgroud)
但是当我输入时gem which ceilingfish-toto.我得到了输出
Can't find ruby library file or shared library ceilingfish-toto
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为如果我去查看我的gems文件夹,我可以看到安装在那里的所有文件
# ls -l /opt/local/lib/ruby/gems/1.8/gems/ceilingfish-toto-0.3.6/
total 48
-rw-r--r-- 1 root admin 1053 14 Feb 17:16 LICENSE
-rw-r--r-- 1 root admin 6166 14 Feb 17:16 README.md
-rw-r--r-- 1 root admin 879 14 Feb 17:16 Rakefile
-rw-r--r-- 1 root admin 6 14 Feb 17:16 VERSION
-rw-r--r-- 1 root admin 2477 14 Feb 17:16 ceilingfish-toto.gemspec
drwxr-xr-x 7 …Run Code Online (Sandbox Code Playgroud) 我正在尝试一些Scala gui编程(我在Scala中的第一个项目,所以我认为我从简单的开始).但我似乎陷入了一些似乎应该相对微不足道的事情.我有一个扩展scala.swing.MainFrame的类,我想检测用户在该窗口具有焦点时按键的时间.有趣的是,我似乎无法找到任何方式让事件发生.
我找到了一个例子,其他人如何解决这个问题:http://houseofmirrors.googlecode.com/svn/trunk/src/src/main/scala/HouseGui.scala但是他们似乎已经恢复使用Java了Swing API,这有点令人失望.有谁知道是否有更惯用的截取事件的方式?
我正在尝试在Wix安装程序中执行自定义操作,以授予绑定到HTTP套接字的权限,在Windows Server 2008上.但是安装程序不能正常工作.
<CustomAction Id="GrantHttpPermission_Cmd" Property="GrantHttpPermission" Value=""[SystemFolder]netsh.exe http add urlacl url=http://+:8732/ user=Service_account"" Execute="immediate"/>
<CustomAction Id="GrantHttpPermission" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="no"/>
...
<InstallExecuteSequence>
<Custom Action="GrantHttpPermission_Cmd" After="CostFinalize"/>
<Custom Action="GrantHttpPermission" After="ConfigureUsers">NOT Installed</Custom>
</InstallExecuteSequence>
Run Code Online (Sandbox Code Playgroud)
在调试模式下运行安装程序我遇到以下故障.我也尝试以管理员身份运行安装程序,输出相同
MSI (s) (14:20) [11:03:00:440]: Executing op: CustomActionSchedule(Action=GrantHttpPermission,ActionType=3073,Source=BinaryData,Target=CAQuietExec,CustomActionData="C:\Windows\SysWOW64\netsh.exe http add urlacl url=http://+:8732/ user=Service_account")
MSI (s) (14:24) [11:03:00:440]: Invoking remote custom action. DLL: C:\Windows\Installer\MSIF794.tmp, Entrypoint: CAQuietExec
CAQuietExec: Error 0x80070002: Command failed to execute.
CAQuietExec: Error 0x80070002: CAQuietExec Failed
CustomAction GrantHttpPermission returned actual error code 1603 (note this may not be 100% …Run Code Online (Sandbox Code Playgroud) 我想使用SDK在TFS中创建一个新的工作项,我想设置项目的工作量估算.我的代码目前看起来像这样
var coll = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://galaxy:8080/tfs/crisp"));
var workItemService = coll.GetService<WorkItemStore>();
var parent = workItemService.GetWorkItem(parentWorkItemId);
WorkItemType workItemType =parent.Project.WorkItemTypes
.Cast<WorkItemType>()
.First(candidateType => candidateType.Name.Equals("Task"));
WorkItem item = workItemType.NewWorkItem();
item.Title = work.Name;
//Set effort estimate here
workItemService.BatchSave(new WorkItem[]{ item });
Run Code Online (Sandbox Code Playgroud)
但是WorkItem的界面似乎没有任何东西允许我设置工作量估算.有谁知道这是怎么做的?