我想将一些表单数据发布到不在我自己的Web应用程序中的指定URL.它具有相同的域,例如"domain.client.nl".Web应用程序有一个URL"web.domain.client.nl",我要发布到的URL是"idp.domain.client.nl".但我的代码什么也没做.....有人知道我做错了吗?
沃特
StringBuilder postData = new StringBuilder();
postData.Append(HttpUtility.UrlEncode(String.Format("username={0}&", uname)));
postData.Append(HttpUtility.UrlEncode(String.Format("password={0}&", pword)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_success={0}&", urlSuccess)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_failed={0}", urlFailed)));
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
// set up request object
HttpWebRequest request;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(WebSiteConstants.UrlIdp);
}
catch (UriFormatException)
{
request = null;
}
if (request == null)
throw new ApplicationException("Invalid URL: " + WebSiteConstants.UrlIdp);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; …Run Code Online (Sandbox Code Playgroud) 我需要为HttpWebRequest对象添加一些自定义标头.如何HttpWebRequest在Windows Phone 7中向对象添加自定义标题 .
我正在启动一个HttpWebRequest,然后检索它的响应.偶尔,我得到500(或至少5 ##)错误,但没有描述.我可以控制两个端点,并希望接收端获得更多信息.例如,我想将异常消息从服务器传递给客户端.这可能使用HttpWebRequest和HttpWebResponse吗?
码:
try
{
HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Credentials = new NetworkCredential(Username, Password);
webRequest.ContentType = "application/x-www-form-urlencoded";
using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
{
if(response.StatusCode == HttpStatusCode.OK)
{
// Do stuff with response.GetResponseStream();
}
}
}
catch(Exception ex)
{
ShowError(ex);
// if the server returns a 500 error than the webRequest.GetResponse() method
// throws an exception and all I get is "The remote server returned an error: (500)."
}
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将非常感激.
我正在尝试连接到使用自签名SSL证书的API.我这样做是使用.NET的HttpWebRequest和HttpWebResponse对象.我得到一个例外:
底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.
我明白这意味着什么.我理解为什么 .NET认为它应该警告我并关闭连接.但在这种情况下,无论如何我都想连接到API,中间人攻击会被诅咒.
那么,我该如何为这个自签名证书添加例外呢?或者是告诉HttpWebRequest/Response不要验证证书的方法?我该怎么办?
我有以下匿名类型:
new {data1 = "test1", data2 = "sam", data3 = "bob"}
Run Code Online (Sandbox Code Playgroud)
我需要一个方法来接受它,并在数组或字典中输出键值对.
我的目标是在HttpRequest中将其用作后期数据,因此我最终将连接到以下字符串中:
"data1=test1&data2=sam&data3=bob"
Run Code Online (Sandbox Code Playgroud) 我对使用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) 我正在使用开源库连接到我的网络服务器.我担心网络服务器的速度非常慢,然后我尝试用Ruby做一个简单的测试,我得到了这些结果
Ruby程序:10个HTTP GET的2.11秒
Ruby程序:100个HTTP GET的18.13秒
C#库:10个HTTP GET的20.81秒
C#库:100个HTTP GET的36847.46seconds
我已经分析并发现问题是这个功能:
private HttpWebResponse GetRawResponse(HttpWebRequest request) {
HttpWebResponse raw = null;
try {
raw = (HttpWebResponse)request.GetResponse(); //This line!
}
catch (WebException ex) {
if (ex.Response is HttpWebResponse) {
raw = ex.Response as HttpWebResponse;
}
}
return raw;
}
Run Code Online (Sandbox Code Playgroud)
标记的行自己完成需要1秒以上,而执行1请求的ruby程序需要0.3秒.我也在127.0.0.1上进行所有这些测试,因此网络带宽不是问题.
什么可能导致这个巨大的减速?
UPDATE
查看更改的基准测试结果.我实际测试了10 GET而不是100,我更新了结果.
我正在使用WCF4.0模板 - REST.我正在尝试创建一个使用流上传文件的方法.
问题总是发生在
Stream serverStream = request.GetRequestStream();
Run Code Online (Sandbox Code Playgroud)
流媒体类:
namespace LogicClass
{
public class StreamClass : IStreamClass
{
public bool UploadFile(string filename, Stream fileStream)
{
try
{
FileStream fileToupload = new FileStream(filename, FileMode.Create);
byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
catch (Exception ex) { throw new Exception(ex.Message); }
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
REST项目:
[WebInvoke(UriTemplate …Run Code Online (Sandbox Code Playgroud) 我知道你们都会回答"使用像Fiddler这样的调试代理服务器",但事情并非那么简单.
这是我的情况:我有一些代码在服务器上运行,在ASP.NET页面代码隐藏(aspx.cs)中,其中(除其他外)建立与另一个服务器的连接,抓取一些东西,然后格式化它并将其返回给浏览器.
问题是其他服务器做错了,所以我希望能够将调试标志传递到页面中(通过查询字符串,例如?debug = true),这样它就会打印出完全原始的 HTTP请求它发送到其他服务器所以我可以看到什么是错的.这段代码在几个地方运行,所以我希望能够在dev,staging或production上传递这个标志,只看到请求,而不必弄清楚生产服务器是否可以与某处存在的代理服务器通信等
你会认为这样做很容易,对吧?所以我觉得我疯了或者别的什么,但我查看了HttpWebRequest及其父类WebRequest的参考资料 - 什么都没有.没有办法.你会认为微软会想到这一点.最接近的是你可以访问"Headers"集合,但是当我尝试它时,它省略了一些非常重要的标题,如"内容长度" - 所以它必须"撒谎"给我(我知道它在撒谎,因为我知道因为远程服务器返回200状态 - 请求成功,它只返回错误/不同/错误的数据)
这是ask-for代码示例:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.whatever.com");
req.Method = ... whatever ...;
... other setup for the request ...
/* At this point we are about to send the request.
What does the raw HTTP request look like? */
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Run Code Online (Sandbox Code Playgroud) c# ×10
httpwebrequest ×10
webclient ×2
.net ×1
asp.net ×1
certificate ×1
debugging ×1
header ×1
http ×1
http-headers ×1
http-post ×1
performance ×1
post ×1
proxy ×1
rest ×1
ssl ×1
wcf ×1