带有GET和POST的HttpWebRequest [英] HttpWebRequest with GET and POST

查看:48
本文介绍了带有GET和POST的HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正打算在我的网站中实现一个api.以下是有关该怎么做的详细信息:

1.获取唯一的AudienceID
2.将用户信息的XML发布到包含#1中的AudienceID的URL.您想将XML文件发布到的URL是http://www.x.com/audiences/{AudienceID}/people.xml.{AudienceID}是我们从步骤1获得的.
3.接收一个StatusCode来查看是否成功添加了新用户.

到目前为止,这是我用于Get的代码.我不认为它是完整的.我不知道从何处开始.请为此提供帮助,我非常感谢所有帮助.


公共函数HttpGet()作为字符串
Dim sb作为StringBuilder
sb.Append("https://www.x.com/audiences.xml")
Dim uri As Uri =新Uri(sb.ToString())

Dim req As HttpWebRequest = HttpWebRequest.Create(uri)
req.Proxy =没什么
req.UserAgent =".NET/2.0"
req.Timeout = 6000
req.ContentType ="application/xml"
req.Accept ="application/xml"

昏暗CombinedPass作为字符串= LOGIN:PASS
昏暗的credCache作为新的CredentialCache
credCache.Add(uri,"Basic",新的NetworkCredential(LOGIN,PASS))
req.Credentials = credCache
req.Headers.Add("Authorization","Basic" + System.Convert.ToBase64String(New ASCIIEncoding().GetBytes(combinedPass)))
结束函数

I am tring to implement an api into my website. Here is the details as to what it is suppose to do:

1. Get a unique AudienceID
2. Post a XML of user information to a URL which includes the AudienceID from #1. The URL you are suppose to post the XML file to is "http://www.x.com/audiences/{AudienceID}/people.xml. The {AudienceID} is what we are getting from step 1.
3. Receive a StatusCode to see if the new user was added successfully.

So far, here is the code i have for the Get. I don''t think it is complete. I have no clue as to where to starts with the post. Please help me with this and i greatly appreciate all the help.


Public Function HttpGet() As String
Dim sb As StringBuilder
sb.Append("https://www.x.com/audiences.xml")
Dim uri As Uri = New Uri(sb.ToString())

Dim req As HttpWebRequest = HttpWebRequest.Create(uri)
req.Proxy = Nothing
req.UserAgent = ".NET/2.0"
req.Timeout = 6000
req.ContentType = "application/xml"
req.Accept = "application/xml"

Dim combinedPass As String = LOGIN:PASS
Dim credCache As New CredentialCache
credCache.Add(uri, "Basic", New NetworkCredential(LOGIN, PASS))
req.Credentials = credCache
req.Headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(New ASCIIEncoding().GetBytes(combinedPass)))
End Function

推荐答案

基于您所写内容的一些想法;

1.如果要向服务器发送大量数据,则应使用POST方法. GET将数据放入具有限制的查询字符串中.

req.Method ="POST";

2.我认为您不能将其发布为文件名(您写了http://www.x.com/audiences/{AudienceID}/people.xml).您更有可能发布到服务器端可执行文件或脚本文件(例如http://www.x.com/audiences/addPeople.aspx).服务器代码将写入您的XML文件.

这里有更多知识渊博的人,他们也许可以告诉您如何直接写文件,但这就是我的做法.
A couple of thoughts based on what you wrote;

1. You should use the POST method if you are sending a lot of data to the server. GET puts the data in the query string, which has limits.

req.Method = "POST";

2. I don''t think you can post it as a file name (you wrote http://www.x.com/audiences/{AudienceID}/people.xml). More likely you are posting to a server-side executable or script file (e.g. http://www.x.com/audiences/addPeople.aspx). The server code would write your XML file.

There are more knowledgeable people here who might be able to tell you how to write a file directly, but this is how I would do it.


这篇关于带有GET和POST的HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆