Tag: httpwebrequest

使用Newtonsoft.json.JsonTextWriter编写Json

我正在使用Newtonsoft.json.JsonTextWriter编写一个json。 这是我的代码: StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonWriter jsonWriter = new JsonTextWriter(sw); jsonWriter.Formatting = Formatting.Indented; jsonWriter.WritePropertyName(“Name”); jsonWriter.WriteValue(“Allan”); 我假设sw有json格式{“Name”:“Allan”}。 如何在一些字符串变量中获取书面文本,以便我可以在我的http请求中使用此json数据?

HttpWebRequest长URI解决方法?

我遇到了HttpWebRequest的问题,如果URI长度超过2048个字符,则请求失败并返回404错误,即使服务器完全能够为URI长的请求提供服务。 我知道这一点,因为如果通过HttpWebRequest提交错误导致错误,直接粘贴到浏览器地址栏时工作正常。 我目前的解决方法是允许用户设置一个兼容性标志,表示将参数作为POST请求发送是安全的,而不是在URI太长的情况下,但这并不理想,因为我使用的协议是RESTful和GET应该用于查询。 此外,没有任何保证协议的其他实现者将接受POSTed查询 在.Net中是否有另一个类具有与HttpWebRequest相同的function,而不受我可以使用的URI长度限制的影响? 我知道WebClient,但我真的不想使用它,因为我需要能够完全控制WebClient限制其能力的HTTP标头。 编辑 因为Shoban要求它: http://localhost/BBCDemo/sparql/?query=PREFIX+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+skos%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23%3E%0D%0APREFIX+dc%3A+%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%0D%0APREFIX+po%3A+%3Chttp%3A%2F%2Fpurl.org%2Fontology%2Fpo%2F%3E%0D%0APREFIX+timeline%3A+%3Chttp%3A%2F%2Fpurl.org%2FNET%2Fc4dm%2Ftimeline.owl%23%3E%0D%0ASELECT+*+WHERE+{%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+dc%3Atitle+%3Ftitle+.%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Ashort_synopsis+%3Fsynopsis-short+.%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Amedium_synopsis+%3Fsynopsis-med+.%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Along_synopsis+%3Fsynopsis-long+.%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Amasterbrand+%3Fchannel+.%0D%0A++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Agenre+%3Fgenre+.%0D%0A++++%3Fchannel+dc%3Atitle+%3Fchanneltitle+.%0D%0A++++OPTIONAL+{%0D%0A++++++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Abrand+%3Fbrand+.%0D%0A++++++++%3Fbrand+dc%3Atitle+%3Fbrandtitle+.%0D%0A++++}%0D%0A++++OPTIONAL+{%0D%0A++++++++%3Chttp%3A%2F%2Fwww.bbc.co.uk%2Fprogrammes%2Fb00n4d6y%23programme%3E+po%3Aversion+%3Fver+.%0D%0A++++++++%3Fver+po%3Atime+%3Finterval+.%0D%0A++++++++%3Finterval+timeline%3Astart+%3Fstart+.%0D%0A++++++++%3Finterval+timeline%3Aend+%3Fend+.%0D%0A++++}%0D%0A}&default-graph-uri=&timeout=30000 以下是编码到查询字符串的以下内容: PREFIX rdf: PREFIX rdfs: PREFIX xsd: PREFIX skos: PREFIX dc: PREFIX po: PREFIX timeline: SELECT * WHERE { dc:title ?title . po:short_synopsis ?synopsis-short . po:medium_synopsis ?synopsis-med . po:long_synopsis ?synopsis-long . po:masterbrand ?channel . po:genre ?genre . ?channel dc:title ?channeltitle . OPTIONAL { po:brand ?brand . […]

HttpWebRequest.GetRequestStream()在Windows 7 / Vista下的SSL连接上超时

我有一个C#windows应用程序(.Net 3.0 Framework),它使用HttpWebRequest调用PHP Web服务。 在Win 7和Vista中 ,如果通过非SSL(即http:// )进行调用,则代码可以正常工作。 当调用更改为调用链接的SSL(https :)版本时,它会在HttpWebRequest.GetRequestStream()超时。 每次都会发生这种情况 当在Windows XP计算机上运行相同的应用程序时,它可以正常使用HTTP或HTTPS URL。 代码确实接受了所有服务器证书代码。 另外,我添加了System.Net日志记录 。 它会写出日志,但因为数据包是加密的,除了Timeout abort语句之外你真的看不到多少。 我也尝试过Fiddler,但再加上加密数据包,我看不太多。 顺便说一句,当我尝试使用Fiddler2解密应用程序挂起时,这样就没有成功。 无论如何,任何帮助将不胜感激。 谢谢。 我会加: 我可以telnet到端口 我尝试以管理员身份运行应用程序 我试过Win XP兼容模式(尝试任何东西) 我已将连接代码单独添加到一个简单的独立应用程序中

使用HTTPWebRequest检查响应时间?

我试图找到我的一些代理的性能。 我在.net中尝试过Ping类,但它不接受端口。 有没有办法检查httpwebrequest的响应时间?

request.GetResponse总是提供超时

我为一个程序创建了一个函数,它在请求类型为GET ,如果它是POST ,它总是在Line HttpWebResponse response = (HttpWebResponse)request.GetResponse();上产生一个Timeoutexception(并且没有达到50s的超时)。 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 我尝试了很多东西,但我没有发现原因,也许有人在这里知道。 编辑:如果有人感兴趣,可以使用它: https : //gist.github.com/4347248 任何帮助将不胜感激。 我的代码是: public ResRequest request(string URL, RequestType typ, CookieCollection cookies, string postdata =””, int timeout= 50000) { byte[] data; Stream req; Stream resp; HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest; request.Timeout = timeout; request.ContinueTimeout = timeout; request.ReadWriteTimeout = timeout; request.Proxy […]

使用WebRequest获取cookie以自动登录Sharepoint Online,获取各种错误

我正在按照本教程远程validationSharepoint Online,但我是在C#中进行的。 Remote authentication in SharePoint Online 我可以从STS获得SAML没有问题,但我似乎无法将此令牌发送到Sharepoint以收回Cookie以便登录。以下是我的代码,请原谅任何明显的错误,我是新来的! //Send cookie to SPO. The token is from STS. byte[] spbyteArray = Encoding.UTF8.GetBytes(theToken); WebRequest sprequest = WebRequest.Create(“https://login.microsoftonline.com/login.srf?wa=wsignin1.0&rpsnv=2&ct=1335885737&rver=6.1.6206.0&wp=MBI&wreply=https%3A%2F%2Fcamida.sharepoint.com%2F_forms%2Fdefault.aspx&lc=1033&id=500046&cbcxt=mai&wlidp=1&guest=1”); sprequest.Method = “POST”; sprequest.ContentLength = spbyteArray.Length; sprequest.ContentType = “application/x-www-form-urlencoded”; sprequest.Headers.GetType().InvokeMember(“ChangeInternal”, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, sprequest.Headers, new object[] { “Host”, “mydomain.sharepoint.com” }); Stream spdataStream = sprequest.GetRequestStream(); spdataStream.Write(spbyteArray, 0, spbyteArray.Length); spdataStream.Close(); […]

WebRequest:如何使用针对此ContentType的WebRequest查找邮政编码=“application / xhtml + xml,text / xml,text / html; 字符集= UTF-8” ?

我首先发布了这个: HttpWebRequest:如何通过带有x-www-form-enclosed的WebRequest在Canada Post找到邮政编码? 。 根据AnthonyWJones的建议,我根据他的建议改变了我的代码。 继续我的询问,我已经注意到加拿大邮政的内容类型更可能是“application / xhtml + xml,text / xml,text / html; charset = utf-8” 。 我的问题是: 我们如何针对这样的内容类型网站进行webrequest? 我们是否必须继续使用NameValueCollection对象? 根据Scott Lance在我之前的问题中慷慨地向我提供了宝贵的信息,WebRequest应该返回任何内容类型的信息类型,我在这里遗漏了什么? 由于内容类型的变化,我是否必须更改我的代码? 这是我的代码,以便更容易理解我的进度。 internal class PostalServicesFactory { /// /// Initializes an instance of GI.BusinessSolutions.Services.PostalServices.Types.PostalServicesFactory class. /// internal PostalServicesFactory() { } /// /// Finds a Canadian postal code for the provided Canadian address. /// […]

最大并发HttpWebRequests数

我正在对Web应用程序进行压力测试,并设置了一个Windows测试程序,该程序可以旋转多个线程,并在每个线程上发送Web请求。 问题是我得到以下输出: 01/09/09 11:34:04 Starting new HTTP request on 10 01/09/09 11:34:04 Starting new HTTP request on 11 01/09/09 11:34:04 Starting new HTTP request on 13 01/09/09 11:34:05 Starting new HTTP request on 14 01/09/09 11:34:05 Starting new HTTP request on 11 01/09/09 11:34:05 11 has finished! 01/09/09 11:34:05 Starting new HTTP request on 13 […]

正确的方法来处理JSON字符串中的&符号发送到REST Web服务

好, 我正在使用System.Runtime.Serialization和DataContractJsonSerialization 。 问题是在请求中我发送了一个带有&字符的属性值。 说, AT&T ,我收到错误的回复: Invalid JSON Data 。 我认为转义将在库内完成,但现在我看到序列化没有触及&符号&字符。 是的,对于JSON格式,这是有效的。 但是这对我的POST请求来说是一个问题,因为我需要将它发送到一个服务器,如果包含一个&符号会响应错误,因此我在这里。 HttpUtility.HtmlEncode位于System.Web库中,因此可以使用Uri.EscapeUriString 。 我这样做是为了尝试,但无论如何,没有它,所有请求都正常工作,除了一个&符号值。 编辑: HttpUtility类移植到Windows Phone SDK,但编码字符串的首选方法仍然是Uri.EscapeUriString 。 首先想到的是弄脏并开始更换会导致服务器出现问题的特殊字符,但是,我想知道,我应该做的另一个解决方案是,它是否有效且“正确”? 我应该告诉我使用 // Convert the string into a byte array. byte[] postBytes = Encoding.UTF8.GetBytes(data); 将JSON转换为byte[]并写入Stream 。 和, request.ContentType = “application/x-www-form-urlencoded”; 作为WebRequest.ContentType 。 那么,我是不是因为某种原因而搞砸了? 谢谢。

错误(407)“需要代理validation”。

我有一个要求…我想从winforms访问一个URL(登录页面是web)。 我必须将凭据传递给该URL,响应应该是经过身份validation的网页(标记)的后果。 我编写了一个函数,它将请求url并返回响应。 但我收到错误代码(407) “需要代理身份validation。” 这是我的代码。 private static void GetPageContent(){ string url = “https://LoginPage.aspx/”; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = “GET”; // If required by the server, set the credentials. //request.Proxy.Credentials = CredentialCache.DefaultCredentials; request.Credentials = new NetworkCredential(“user1”, “testuser#”); // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. Console.WriteLine(response.StatusDescription); // Get the stream […]