将数据发送到HTTP标头 [英] Send Data Into HTTP Header
问题描述
我有以下c ++代码片段,并且我不确定从哪里开始将其转换为vb.net等效项.此示例使用我不熟悉的Siber HTTP库以c ++编写.该代码用于在服务器中添加/更改/删除用户 在Windows Server 2008上运行的类似ftp的产品.我对vb.net代码不熟悉,该代码可能会通过其标头集激活此http代码.有人可以帮忙!使用.Net 3.5,而不是4.我不清楚这些属性中的某些属性在实际中是否可用 vb.net属性,或者如果这些都是我需要使用纯文本以某种方式设置的名称/值对,请使用正确的vb.net代码来激活http.
SibHttpConnectoid http_conn;
http_conn.m_sUrl = _T(" http://localhost:33334 &";);
http_conn.m_sUserID = _T("admin-user"); 跨度>
http_conn.m_sPassword = _T("admin-password"); 跨度>
SibHttpTxn txn;
txn.m_sSendHeadersA =" \
管理方法:AddUser \ r \ n \ >
用户ID:new-user \ r \ n \ >
密码:new-pwd \ r \ n \ >
home-path: file:///c:\\ user-home \ r \ n ";
bool rc = DoSibHttpTxnAndClose(txn,http_conn,"Manage",NULL, sErr);
德里克,
我对C ++非常熟悉,但是上面的代码使用了一个库,
使用vb.net发出http请求非常容易,只需使用.net上的webrequest对象,
这是您的起点,您也可以检查webclient对象.
http://msdn.microsoft.com/zh-cn/library/debx8sh9%28v=VS.85%29.aspx
Public Shared Sub Main() ' Create a request using a URL that can receive a post. Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ") ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim postData As String = "This is a test that posts this string to a Web server." Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = request.GetResponse() ' Display the status. Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. Console.WriteLine(responseFromServer) ' Clean up the streams. reader.Close() dataStream.Close() response.Close() End Sub
I have the following c++ code snipplet, and I am not sure where to begin converting this to a vb.net equivalent. This example is written in c++ using the Siber HTTP library, which I am not familiar with. The code is to add/change/delete users in a server ftp-like product that runs on Windows Server 2008. I am not familiar with the vb.net code that could activate this http code with its header set. Can somebody help! Using .Net 3.5, not 4. I am not clear if some of these properties might be available in actual vb.net properties, or if these are all name/value pairs that I need to set somehow using plain text, with the proper vb.net code that activates http.
SibHttpConnectoid http_conn;
http_conn.m_sUrl = _T("http://localhost:33334");
http_conn.m_sUserID = _T("admin-user");
http_conn.m_sPassword = _T("admin-password");
SibHttpTxn txn;
txn.m_sSendHeadersA = "\
Manage-method: AddUser\r\n\
userid: new-user\r\n\
password: new-pwd\r\n\
home-path: file:///c:\\user-home\r\n";
bool rc = DoSibHttpTxnAndClose(txn, http_conn, "Manage", NULL, sErr);
解决方案Hi Derek,
I m quite familar with C++ but the code above uses a library,
its very easy to make http request with vb.net just use the webrequest object on .net,
here is a starting point for you,you can check webclient object too.
http://msdn.microsoft.com/en-us/library/debx8sh9%28v=VS.85%29.aspx
Public Shared Sub Main() ' Create a request using a URL that can receive a post. Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ") ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim postData As String = "This is a test that posts this string to a Web server." Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = request.GetResponse() ' Display the status. Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. Console.WriteLine(responseFromServer) ' Clean up the streams. reader.Close() dataStream.Close() response.Close() End Sub
这篇关于将数据发送到HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文