Tag: httpwebrequest

.NET相当于curl将文件上传到REST API?

我需要将一个ics文件上传到REST API。 给出的唯一示例是curl命令。 用于使用curl上传文件的命令如下所示: curl –user {username}:{password} –upload-file /tmp/myappointments.ics http://localhost:7070/home/john.doe/calendar?fmt=ics 如何在C#中使用HttpWebRequest执行此操作? 另请注意,我可能只将ics作为字符串(而不是实际文件)。

c#HttpWebRequest POST’ing失败

所以我想尝试将内容发布到网络服务器上。 System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(“url”); System.String Content = “id=” + Id; EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content); EventReq.Method = “POST”; EventReq.ContentType = “application/x-www-form-urlencoded”; System.IO.StreamWriter sw = new System.IO.StreamWriter(EventReq.GetRequestStream(), System.Text.Encoding.UTF8); sw.Write(Content); sw.Flush(); sw.Close(); 看起来没问题,我根据ENCODED数据的大小设置内容长度…无论如何,它在sw.flush()失败,“要写入流的字节数超过指定的内容长度大小” StreamWriter在我背后做了一些魔法,我不知道吗? 有没有办法让我了解StreamWriter正在做什么?

如何使用HttpWebRequest在POST中转义URL编码数据

我正在尝试将URL编码的post发送到用PHP实现的REST API。 POST数据包含两个用户提供的字符串: WebRequest request = HttpWebRequest.Create(new Uri(serverUri, “rest”)); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded; charset=UTF-8”; request.Headers.Add(“Content-Transfer-Encoding”, “binary”); // Form the url-encoded credentials we’ll use to log in StringBuilder builder = new StringBuilder(); builder.Append(“user=”); builder.Append(user); builder.Append(“&password=”); builder.Append(password); byte[] credentials = Encoding.UTF8.GetBytes(builder.ToString()); // Write the url-encoded post data into the request stream. request.ContentLength = credentials.Length; using […]

System.Net.HttpWebResponse.GetResponseStream()在WebException中返回截断的主体

出于某些原因,我不了解对特定网站( https://learningnetwork.cisco.com/people/mrollins?view=profile )的请求会产生一个reqsponse对象,其响应流包含网站的主干版本。 流在65536字节之后结束,等于2 ^ 16字节。 我认为这是一个可疑的数字。 这些请求抱怨内部服务器错误,我对此表示赞同,因为我已经validation了webbrowsers能够理解这个响应,并且完整的html包含在服务器的响应中。 (使用提琴手 ) 我已经发现此前记录的问题,由于它在本说明中结束的简单原因,这是不能令人满意的: “我想我不得不希望错误不超过65536个字符……” 它确实如此。 解决方法很受欢迎,或者如果有人知道一个好的修复也很好。 using System; using System.IO; using System.Net; using System.Web.UI; namespace Example { public partial class _Default : Page { protected void Page_Load(object senderHidden, EventArgs eHidden) { //ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; var cookieContainer […]

C#网络凭据没有传递给服务器?

编辑:使用: byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(user + “:” + password); wr.Headers[“Authorization”] = “Basic ” + Convert.ToBase64String(authBytes); 似乎工作正常。 我有一个与CMS通信的应用程序。 我目前正在尝试让这个客户端能够使用“POST”方法将文本/ xml数据上传到CMS。 我可以通过使用curl完美地传递这个: curl -u user:password -H “Content-Type:text/xml” -d “myXML” serverURL 但是,尝试在C#中使用HttpWebRequest我无法让服务器返回我想要的内容。 所以我解雇了Wireshark并查看了实际传递的内容,它几乎完全相同,除了使用curl时我可以看到: Authorization: Basic =\r\n Credentials: user:password 在HTTP标头字段中,在我的客户端的输出中,这些标头字段根本不存在。 (“凭据:”实际上并不是纯文本,它是“授权:”的子树 – 所以我不知道它从何处获取,但用户名和密码是正确的。) 我试图用来设置webrequest凭据的C#代码是这样的: NetworkCredential myCred = new NetworkCredential( user, password, serverURL); CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri(serverURL), […]

使用Thread.Abort杀死HttpWebRequest对象

所有,我试图使用类似于下面的代码的方法取消两个并发的HttpWebRequests(以伪ish C#显示)。 Main方法创建两个创建HttpWebRequests的线程。 如果用户希望,他们可以通过单击按钮然后调用Abort方法来中止请求。 private Thread first; private Thread second; private string uri = “http://somewhere”; public void Main() { first = new Thread(GetFirst); first.Start(); second = new Thread(GetSecond); second.Start(); // Some block on threads… like the Countdown class countdown.Wait(); } public void Abort() { try { first.Abort(); } catch { // do nothing } try […]

C#HttpWebResponse Comet问题

我想知道如何阅读与HttpWebRequest和HttpWebResponse的持久连接。 问题似乎是GetResponseStream()函数在返回之前等待关闭服务器连接。 有没有其他简单的方法来读取彗星连接? 不起作用的示例。 // get the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { // fill our buffer count = resStream.Read(buf, 0, buf.Length); // as long as we read something we want to print it if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Debug.Write(tempString); […]

从HttpWebRequest / Response获取底层tcp连接

我正在尝试获取有关当我连接到比HttpWebRequest和HttpWebResponse给我更低级别的网站时发生的事情的更多信息。 我正在使用C#。 我希望能够看到有关dns查找的信息以及建立连接所花费的时间(如果建立了新连接)。 HttpWebRequest和HttpWebResponse工作在比这更高的级别,我想询问是否有获取底层TcpClient对象(或他们使用的任何低级别对象)的方法。 如果不可能,那么有没有办法获取和操作.net维护的连接列表,而不通过HttpWebRequest或HttpWebResponse获取它? 我无法更改我正在使用TcpClient的应用程序,因为可靠地实现所有http内容太费时间了。

无法从传输连接读取数据:控制台应用程序中的连接已关闭错误

我在控制台应用程序中有这个代码,它在一个循环中运行 try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search); request.Headers.Add(“Accept-Language”, “de-DE”); request.Method = “GET”; request.Accept = “text/html”; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { string html = reader.ReadToEnd(); FindForMatch(html, url); } } } catch (Exception ex) { throw new Exception(ex.Message); } 经过几次循环就给出了 无法从传输连接读取数据:连接已关闭 错误。 知道为什么会这样吗? 感谢名单..

AWS API网关签名

我正在尝试向亚马逊网关签署我的请求。 但每当我尝试发送POST请求时,它都会告诉我我的签名已过期。 任何想法将不胜感激。