我正在使用WebClient该类将一些数据发布到Web表单.我想获取表单提交的响应状态代码.到目前为止,我已经发现如果有异常,如何获取状态代码
Catch wex As WebException
If TypeOf wex.Response Is HttpWebResponse Then
msgbox(DirectCast(wex.Response, HttpWebResponse).StatusCode)
End If
Run Code Online (Sandbox Code Playgroud)
但是,如果表单提交成功并且没有抛出异常,那么我将不知道状态代码(200,301,302,...)
有什么方法可以在没有抛出异常时获取状态代码?
PS:我不想使用httpwebrequest/httpwebresponse
我有一些我需要转换为C#的JavaScript代码.我的JavaScript代码将一些JSON发布到已创建的Web服务.此JavaScript代码工作正常,如下所示:
var vm = { k: "1", a: "2", c: "3", v: "4" };
$.ajax({
url: "http://www.mysite.com/1.0/service/action",
type: "POST",
data: JSON.stringify(vm),
contentType: "application/json;charset=utf-8",
success: action_Succeeded,
error: action_Failed
});
function action_Succeeded(r) {
console.log(r);
}
function log_Failed(r1, r2, r3) {
alert("fail");
}
Run Code Online (Sandbox Code Playgroud)
我正在试图找出如何将其转换为C#.我的应用程序使用的是.NET 2.0.据我所知,我需要做以下事情:
using (WebClient client = new WebClient())
{
string json = "?";
client.UploadString("http://www.mysite.com/1.0/service/action", json);
}
Run Code Online (Sandbox Code Playgroud)
我现在有点卡住了.我不确定json应该是什么样的.我不确定是否需要设置内容类型.如果我这样做,我不知道该怎么做.我也看到了UploadData.所以,我不确定我是否使用了正确的方法.从某种意义上说,我的数据序列化是我的问题.
谁能告诉我我在这里失踪了什么?
谢谢!
以下代码:
var text = (new WebClient()).DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20"));
Run Code Online (Sandbox Code Playgroud)
导致变量text包含字符串等许多内容
"$κ$ -Minkowski空间,标量场和洛伦兹不变性问题"
但是,当我在Firefox中访问该URL时,我得到了
$κ$ -Minkowski空间,标量场和洛伦兹不变性问题
这实际上是正确的.我也试过了
var data = (new WebClient()).DownloadData("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
var text = System.Text.UTF8Encoding.Default.GetString(data);
Run Code Online (Sandbox Code Playgroud)
但这给了同样的问题.
我不确定这里的错误在哪里.Feed是否是UTF8编码的,浏览器足够聪明,可以解决这个问题,但不是WebClient吗?Feed是否正确UTF8编码,但是WebClient以其他方式失败了?我该怎么做才能缓解这种情况?
我对使用C#比较陌生,并且有一个应用程序可以在网站上读取部分源代码.这一切都有效; 但问题是有问题的页面要求用户登录才能访问此源代码.我的程序需要一种最初将用户登录到网站的方式 - 完成后,我将能够访问和阅读源代码.
需要登录的网站是:mmoinn.com/index.do?PageModule=UsersLogin
我一整天都在寻找关于如何做到这一点并尝试过的例子,但没有运气.
提前致谢
我知道将证书添加到HttpWebRequest非常简单.但是,我还没有找到使用WebClient进行等效的方法.基本上,我想使用WebClient发送带有特定证书的POST.
您将如何使用WebClient完成此确切代码:
var request = (HttpWebRequest) WebRequest.Create("my-url");
request.Method = "POST";
request.ClientCertificates.Add(new X509Certificate()); //add cert
Run Code Online (Sandbox Code Playgroud) 我目前正在与第三方创建的系统集成.该系统要求我使用XML/HTTPS发送请求.第三方发给我证书,我安装了它
我使用以下代码:
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
var response = client.UploadData(address, "POST", encoding.GetBytes(msg));
}
Run Code Online (Sandbox Code Playgroud)
此代码返回以下内容WebException:
底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.
更新因为它是我正在反对的测试服务器,证书不受信任且验证失败...要在测试/调试环境中绕过它,请创建一个新的ServerCertificateValidationCallback
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff);
Run Code Online (Sandbox Code Playgroud)
这是我的"假"回调
private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
使用C#在FTP服务器上创建目录的简单方法是什么?
我想出了如何将文件上传到现有的文件夹,如下所示:
using (WebClient webClient = new WebClient())
{
string filePath = "d:/users/abrien/file.txt";
webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想上传到users/abrien,我会WebException说文件不可用.我认为这是因为我需要在上传文件之前创建新文件夹,但WebClient似乎没有任何方法可以实现.
好吧我编辑了我的代码我没有得到错误,但messageBox.Show没有返回任何空框.也许我需要在referrer字符串中添加一些东西?我不明白什么是推荐人,我应该把它放在那里.我有一个密钥已经在我的代码中使用它.密钥是一个长字符串,我在我的代码中使用它我不与引用者一起使用.为什么它倾向于翻译"hi"这个词?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private JavaScriptSerializer _Serializer = new JavaScriptSerializer();
public Form1()
{
InitializeComponent();
string f = TranslateText("hi", "English", "German", "", "");
MessageBox.Show(f);
}
private void Form1_Load(object sender, EventArgs e)
{
}
public string TranslateText(string inputText, string sourceLanguage, string destinationLanguage, string referrer, string apiKey)
{
string requestUrl …Run Code Online (Sandbox Code Playgroud) 当我创建一个WebClient来使用一些RESTful xml时,我可以通过两种方式指定unicode编码:
WebClient wc = new WebClient ();
wc.Encoding = Encoding.UTF8;
wc.Encoding = UTF8Encoding.UTF8;
Run Code Online (Sandbox Code Playgroud)
哪个更正/更好?
今天早上我收到了可怕的'Twitter REST API v1不再有效.请迁移到API v1.1.我的一些网站出错.
以前我一直在使用javascript/json来调用http://api.twitter.com/1/statuses/user_timeline.json?显示时间轴.
由于这不再可用,我需要采用新的1.1 API流程.
我需要使用HttpWebRequest对象而不是第三方应用程序执行以下操作: