使用Httpwebrequest提交multipart / form-data [英] Using Httpwebrequest to Submit multipart/form-data
问题描述
我正在尝试在vb.net中编写一个程序,以自动在网站上填写一个
系列表格。我需要三种形式来按顺序填写
。第一个是urlencoded。我的程序是
能够很好地填写那个。
第二种形式是multipart / form-data。不幸的是,我没有能够以一种让服务器满意的方式填补这一点。
我设置了这份表格的副本在我的网站上,以便我可以看到
确切地说是浏览器发送到服务器并将其与我的程序发送的内容进行比较。我在程序中看到了一些错误,并且我已经修复了它们,但真正的网站仍然拒绝我的程序'
提交。
>
据我所知,我的程序提交的内容是与浏览器提交的内容相同的
。有没有其他可能导致我问题的原因?
感谢您的帮助。
-
格雷格
----
greg -at- spencersoft -dot- com
I''m trying to write a program in vb.net to automate filling out a
series of forms on a website. There are three forms I need to
fill out in sequence. The first one is urlencoded. My program is
able to fill that one out just fine.
The second form is multipart/form-data. Unfortunately, I haven''t
been able to fill that out in a way that makes the server happy.
I set up a copy of this form at my web site so that I could see
exactly what a browser sends to the server and compare that to
what my program sends. I saw a few mistakes in my program and I
fixed them, but the real web site still rejects my program''s
submissions.
As best I can tell, the contents that my program submits are
identical to the contents submitted by a browser. Is there
anything else that might be causing my problem?
Thanks for any help.
--
Greg
----
greg -at- spencersoft -dot- com
推荐答案
Gregory A Greenman写道:
Gregory A Greenman wrote:
我正在尝试在vb.net中编写一个程序,以自动填写网站上的一系列表格。我需要按顺序填写三种表格。第一个是urlencoded。我的程序能够很好地填写那个。
第二种形式是multipart / form-data。不幸的是,我没有能够以一种让服务器满意的方式填补这一点。
我在我的网站上设置了这份表格的副本,这样我就可以确切地了解浏览器发送给服务器的内容,并将其与我的程序发送的内容进行比较。我在程序中看到了一些错误,并且我已经修复了它们,但真实的网站仍然拒绝我的程序提交的内容。
我最好能告诉你,我的程序提交的内容与浏览器提交的内容相同。还有什么可能导致我的问题吗?
I''m trying to write a program in vb.net to automate filling out a
series of forms on a website. There are three forms I need to
fill out in sequence. The first one is urlencoded. My program is
able to fill that one out just fine.
The second form is multipart/form-data. Unfortunately, I haven''t
been able to fill that out in a way that makes the server happy.
I set up a copy of this form at my web site so that I could see
exactly what a browser sends to the server and compare that to
what my program sends. I saw a few mistakes in my program and I
fixed them, but the real web site still rejects my program''s
submissions.
As best I can tell, the contents that my program submits are
identical to the contents submitted by a browser. Is there
anything else that might be causing my problem?
好吧,从10000'分析你的问题并不是那么容易......你提供了
更多细节(代码,错误消息......)?
干杯,
- -
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
什么错误信息是它给你?是否正在寻找一套指定的
凭证等?
Sean McCormack
开源.NET
http://www.adapdev.org
Gregory A Greenman写道:
What error message is it giving you? Is it looking for a specified set of
credentials, etc.?
Sean McCormack
Open Source for .NET
http://www.adapdev.org
"Gregory A Greenman" wrote:
我正在尝试在vb.net中编写一个程序,以自动填写网站上的一系列表格。我需要按顺序填写三种表格。第一个是urlencoded。我的程序能够很好地填写那个。
第二种形式是multipart / form-data。不幸的是,我没有能够以一种让服务器满意的方式填补这一点。
我在我的网站上设置了这份表格的副本,这样我就可以确切地了解浏览器发送给服务器的内容,并将其与我的程序发送的内容进行比较。我在程序中看到了一些错误,并且我已经修复了它们,但真实的网站仍然拒绝我的程序提交的内容。
我最好能告诉你,我的程序提交的内容与浏览器提交的内容相同。还有什么可能导致我的问题吗?
感谢您的帮助。
-
Greg
--- -
greg -at- spencersoft -dot- com
I''m trying to write a program in vb.net to automate filling out a
series of forms on a website. There are three forms I need to
fill out in sequence. The first one is urlencoded. My program is
able to fill that one out just fine.
The second form is multipart/form-data. Unfortunately, I haven''t
been able to fill that out in a way that makes the server happy.
I set up a copy of this form at my web site so that I could see
exactly what a browser sends to the server and compare that to
what my program sends. I saw a few mistakes in my program and I
fixed them, but the real web site still rejects my program''s
submissions.
As best I can tell, the contents that my program submits are
identical to the contents submitted by a browser. Is there
anything else that might be causing my problem?
Thanks for any help.
--
Greg
----
greg -at- spencersoft -dot- com
在文章< uD ************ **@tk2msftngp13.phx.gbl>,
jo ********* @ gmx。 net 说...
In article <uD**************@tk2msftngp13.phx.gbl>,
jo*********@gmx.net says...
Gregory A Greenman写道:
Gregory A Greenman wrote:
我正在尝试在vb.net中编写一个程序来自动填写一个
网站上的一系列表格。我需要按顺序填写三种表格。第一个是urlencoded。我的程序能够很好地填写那个。
第二种形式是multipart / form-data。不幸的是,我没有能够以一种让服务器满意的方式填补这一点。
我在我的网站上设置了这份表格的副本,这样我就可以确切地了解浏览器发送给服务器的内容,并将其与我的程序发送的内容进行比较。我在程序中看到了一些错误,并且我已经修复了它们,但真实的网站仍然拒绝我的程序提交的内容。
我最好能告诉你,我的程序提交的内容与浏览器提交的内容相同。还有什么可能导致我的问题吗?
I''m trying to write a program in vb.net to automate filling out a
series of forms on a website. There are three forms I need to
fill out in sequence. The first one is urlencoded. My program is
able to fill that one out just fine.
The second form is multipart/form-data. Unfortunately, I haven''t
been able to fill that out in a way that makes the server happy.
I set up a copy of this form at my web site so that I could see
exactly what a browser sends to the server and compare that to
what my program sends. I saw a few mistakes in my program and I
fixed them, but the real web site still rejects my program''s
submissions.
As best I can tell, the contents that my program submits are
identical to the contents submitted by a browser. Is there
anything else that might be causing my problem?
好吧,从10000'分析你的问题并不是那么容易......你能提供吗? />更多细节(代码,错误消息,......)?
Well, analyzing your problem from 10000'' isn''t that easy... can you provide
more details (code, error messages, ...)?
好的。我收到了远程服务器返回错误:(500)
内部服务器错误。
我有一个表单左上角有两个文本框,
txtHalfID和txtHalfPassword。
右上角有三个文本框,txtISBN,txtDescription和txtPrice。右上方还有一个下拉组合,cmbCondition。
屏幕的下半部分有一个只读的多行文字
名为txtResponse的框,其中显示来自服务器的原始HTML
。
左侧文本框下方是一个按钮, btnSignOn。如果您点击
该按钮,它将使用ID和
密码在上面的文本框中登录Half.com。这部分对我来说很好。
我可以看到欢迎来到eBay txtResponse中的页面。
右侧框下方是一个名为btnListBook的按钮。当我点击它时,它应该按顺序在half.com上调用四个网页。
首先它调用 http://half.ebay.com/help/sell_books.cfm 。它用txtISBN在那里用字段填写
。
接下来它调用 http://half.ebay.com/cat/sell/pmsearch.cgi 。它用
用txtDescription和cmbCondition填写表格。
这个表格允许文件上传,所以它是一个多部分形式。虽然
我的程序输出看起来不错,但是当我提交此表单时,我收到服务器错误
。
这是代码:
- 启动代码--------------------------- ------------------------
Imports System.Net
公共类SBM
继承System.Windows.Forms.Form
Dim cc作为新CookieCollection
Const encURL As Integer = 0
Const encMulti As Integer = 1
Private Sub SBM_Load(ByVal sender As System.Object,ByVal e
As System.EventArgs)处理MyBase.Load
txtHalfID.Text =""
txtHalfPassword.Text =""
txtISBN。 Text =" 0764560255"
txtDescription.Text =" good good"
txtPrice.Text =" 99.99"
with cmbCondition
.Items.Add(新条件(全新,830))
.Items.Add(新增条件(Like New,840))
.Items.Add(新条件(非常好,849))
.Items.Add(新条件(好,859))
.Items.Add(新条件(可接受,864))
.SelectedIndex = 0
结束
cmbCondition.SelectedIndex = 0
End Sub
Private Sub btnSignOn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)处理btnSignOn.Click
Dim HWRequest As HttpWebRequest
Dim strURL As String
Dim HWParameters As Parameters
Dim intFound As Integer
HWRequest = GetRequest
(" https://signin.ebay.com/ws/eBayISAPI.dll?SignIn& UsingSSL = 1
& co_partnerid = 2& siteid = 20" )
strURL =" https://signin.half.ebay.com/ws/eBayISAPI.dll"
HWParameters = ReadResponse(HWRequest, strURL)
intFound = 0
每个p作为HWParameters中的参数
选择Case p.Name
Case" userid"
p.Value = txtHalfID.Text
intFound + = 1
Case"传递
p.Value = txtHalfPassword.Text
intFound + = 1
结束选择
如果intFound = 2那么
退出
结束如果
下一页
HWRequest = PostRequest (strURL,HWParameters,encURL)
GetResponse(HWRequest)
End Sub
Private Sub btnListBook_Click(ByVal sender As System。对象,
ByVal e As System.EventArgs)处理btnListBook.Click
Dim strPostData As String
Dim HWRequest As HttpWebRequest
Dim strURL As String
Dim HWParameters As Parameters
Dim intFound As Integer
Dim pr As Parameter
尝试
HWRequest = GetRequest
(&qu ot; http://half.ebay.com/help/sell_books.cfm")
strURL =" http://half.ebay.com/cat/sell/pmsearch.cgi"
HWParameters = ReadResponse(HWRequest,strURL)
intFound = 0
每个p作为参数在HWParameters中
选择案例p.Name
案例" p_code"
p.Value = txtISBN.Text
intFound + = 1
结束选择
如果intFound = 1那么
退出
结束如果
下一页
HWRequest = PostRequest(strURL,HWParameters,encURL)
strURL =" /cat/sell/save_new_listing.cgi"
HWParameters = ReadResponse(HWRequest,strURL)
pr =新参数
pr .Name =" x"
pr.Value = 20
pr.Type = Parameter.Input
HWParameters.Add(pr)
pr =新参数
pr.Name =" y"
pr.Value = 20
pr。 Type = Para meter.Input
HWParameters.Add(pr)
intFound = 0
每个p作为HWParameters中的参数
Select Case p.Name
Case" notes"
p.Value = txtDescription.Text
intFound + = 1
案例"条件"
p.Value = cmbCondition.Items
(cmbCondition.SelectedIndex).ItemData
intFound + = 1
结束选择
如果intFound = 2那么
退出
结束如果
下一页
strURL =
" http://half.ebay.com/cat/sell/save_new_listing .cgi"
HWRequest = PostRequest(strURL,HWParameters,
encMulti)
strURL =" sell .jsp"
HWRequest.Referer =
" http://half.ebay.com/cat/sell/pmsearch.cgi"
''在此调用中生成内部服务器错误
''到ReadResponse
HWParameters = ReadResponse(HWReq) uest,strURL)
intFound = 0
每个p作为HWParameters中的参数
选择案例p.Name
Case" itemPrice"
p.Value = txtPrice.Text
intFound + = 1
End Select
如果intFound = 1那么
退出
结束如果
下一页
>
strURL =" http://half.ebay.com/cat/sell/sell.jsp"
HWRequest = PostRequest(strURL,HWParameters,encURL)
GetResponse(HWRequest)
MessageBox.Show(" Success!",MsgBoxStyle.OKOnly," Book
发表于)
Catch ex As Exception
MessageBox.Show(" Error:" &安培; ex.Message,Error !!!,
MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
MsgBox(Half.com似乎已更改其发布的
程序。因此,此版本的SBM无法列出
上的书籍。,MsgBoxStyle.Exclamation,Half.com列表问题 ;)
结束尝试
结束子
私函数GetRequest(ByVal strURL As String)As
HttpWebRequest
GetRequest = CreateRequest(strURL)
GetRequest.Method =" GET"
结束功能
>
私有函数PostRequest(ByVal strURL As String,ByVal
PostParameters As Parameters,ByVal intType as Integer)As
HttpWebRequest
Dim编码As New System.Text.ASCIIEncoding
Dim byte1 As Byte()
Dim newStream As System.IO.Stream
Dim strData As字符串
Const strBoundary As String =
" ---------------- ----------- 7d4285126106b0"
PostRequest = CreateRequest(strURL)
PostRequest.Method =" POST"
如果intType = encURL那么
PostRequest.ContentType =" application / x-www-form-
urlencoded"
Else
PostRequest.ContentType =" multipart / form-data,
boundary =" &安培; strBoundary
结束如果
strData = BuildRequestString(PostParameters,intType,
strBoundary)
PostRequest.ContentLength = strData.Length
byte1 = encoding.GetBytes(strData)
newStream = PostRequest.GetRequestStream >
newStream.Write(byte1,0,byte1.Length)
newStream.Close()
结束功能
私有函数BuildRequestString(ByVal RequestParameters
作为参数,ByVal intType为整数,ByVal strBoundary为
字符串)
BuildRequestString ="" ;
Select Case intType
案例包含
每个p作为RequestParameters中的参数
BuildRequestString & = p.Name.Trim& " =" &
p.Value.Trim& "&"
下一页
BuildRequestString = Mid(BuildRequestString,1,
Len(BuildRequestString) - 1)
Case encMulti
''为多部分表单生成的请求字符串是
''如下所示
For Each p作为RequestParameters中的参数
BuildRequestString& =" - " &安培; strBoundary&
vbCrLf& 内容 - 处置:表格 - 数据;名称= QUOT;"" &安培; p.Name.Trim
& """"
Select Case p.Type
Case Parameter.File
BuildRequestString& =" ;;
filename =""""" &安培; vbCrLf& Content-Type:application / octet-
stream &安培; vbCrLf& vbCrLf& vbCrLf
Case Parameter.Input
BuildRequestString& = vbCrLf& vbCrLf
& p.Value& vbCrLf
结束选择
下一页
BuildRequestString& =" - " &安培; strBoundary& " - "
结束选择
结束功能
私有函数CreateRequest(ByVal strURL As String)As
HttpWebRequest
Dim Uri As Uri
Uri = New Uri(strURL)
CreateRequest = HttpWebRequest。创建(Uri)
CreateRequest.AllowAutoRedirect = True
CreateRequest.CookieContainer =新CookieContainer
如果cc.Count> 0然后
CreateRequest.CookieContainer.Add(cc)
结束如果
结束功能
Private Sub GetResponse(ByVal ReadRequest As HttpWebRequest)
Dim HWResponse As HttpWebResponse
Dim cookie as Cookie
HWResponse = ReadRequest.GetResponse ()
如果HWResponse.Cookies.Count> 0然后
每个cookie在HWResponse.Cookies
cc.Add(cookie)
下一页
结束如果
''调试
Dim sr As System.IO.StreamReader
Dim strResult As String
sr =新的System.IO.StreamReader
(HWResponse.GetResponseStream())
txtResponse.Text = sr.ReadToEnd >
sr.Close()
''调试
End Sub
私人函数ReadResponse (ByVal ReadRequest As
HttpWebRequest,ByVal ReadURL As String)作为参数
Dim HWResponse As HttpWebResponse
Dim cookie as Cookie
Dim sr As System.IO.StreamReader
Dim strResult As String
Dim intTagPos As Integer
Dim intLength As Integer
Dim intURLPos As Integer
Dim strTag As String
Dim blnFormFound As Boolean
Dim blnMoreForms As Boolean
Dim intPos As Integer
Dim strName As String
Dim strValue As String
Dim ReadParameter As Parameter
Dim intType As Integer
Dim intInputPos作为整数
Dim intTextPos作为整数
Dim intSelectPos作为整数
Dim blnTagFound作为布尔值
ReadResponse =新参数
''下一行生成内部服务器错误
HWResponse = ReadRequest.GetResponse()
如果HWResponse.Cookies.Count> 0然后
每个cookie在HWResponse.Cookies
cc.Add(cookie)
下一页
结束如果
sr =新的System.IO.StreamReader
(HWResponse.GetResponseStream())
strResult = sr .ReadToEnd
''调试
txtResponse.Text = strResult
''调试
blnFormFound = False
blnMoreForms = True
intTagPos = 1
而不是blnFormFound和blnMoreForms
intTagPos = InStr(intTagPos,strResult.ToUpper,
"< FORM",CompareMethod.Text)
如果intTagPos<> 0然后
intLength = InStr(intTagPos,strResult,">",
CompareMethod.Text) - intTagPos
strTag = strResult .substring(intTagPos,
intLength)
intURLPos = InStr(1,strTag.ToUpper,
ReadURL.ToUpper,CompareMethod .Text)
如果是intURLPos<> 0然后
intLength = InStr(intTagPos,
strResult.ToUpper,"< / FORM>",CompareMethod.Text) - intTagPos
strResult = strResult.Substring(intTagPos,
intLength)
blnFormFound = True
Else
intTagPos + = 1
结束如果
否则
blnMoreForms = False
结束如果
结束时
如果blnFormFound那么
intTagPos = 1
虽然intTagPos<> 0
intInputPos = InStr(intTagPos,strResult.ToUpper,
"< INPUT",CompareMethod.Text)
intTextPos = InStr( intTagPos,strResult.ToUpper,
"< TEXTAREA",CompareMethod.Text)
intSelectPos = InStr(intTagPos,
strResult.ToUpper ,"< SELECT",CompareMethod.Text)
intTagPos = IIf(intTextPos<> 0和intTextPos<
intInputPos,intTextPos, intInputPos)
intTagPos = IIf(intSelectPos<> 0和
intSelectPos< intTagPos,intSelectPos,intTagPos)
intType = Parameter.Input
如果intTagPos<> 0然后
intLength = InStr(intTagPos,strResult,">",
CompareMethod.Text) - intTagPos
strTag = strResult .substring(intTagPos,
intLength)
intPos = InStr(1,strTag.ToUpper,
" TYPE = SUBMIT" ;,CompareMethod.Text)
如果intPos = 0那么
intPos = InStr(1,strTag.ToUpper,
" TYPE =" ;" SUBMIT"",CompareMethod.Text)
结束如果
如果intPos = 0那么
intPos = InStr(1,strTag.ToUpper,
" NAME =""",CompareMethod.Text)
如果intPos = 0则
intPos = InStr(1,strTag.ToUpper,
" NAME =" ;, CompareMethod.Text)
intLength = InStr(intPos + 5,strTag,
"",CompareMethod.Text) - intPos - 5
strName = strTag.Substring(intPos +
4 ,intLength)
Else
intLength = InStr(intPos + 6,strTag,
"""",CompareMethod.Text) - intPos - 6
5,intLength)<如果
如果intPos<> 0然后
intPos = InStr(1,strTag.ToUpper,
" VALUE =""",CompareMethod.Text)
>
如果intPos = 0那么
strValue =""
否则
intLength = InStr(intPos + 7,
strTag,"""",CompareMethod.Text) - intPos - 7
strValue = strTag.Substring
(intPos + 6,intLength)
结束如果
intPos = InStr(1,strTag.ToUpper,
" TYPE =" " FILE"",CompareMethod.Text)
如果intPos = 0那么
intPos = InStr(1,strTag.ToUpper,
" TYPE = FILE",CompareMethod.Text)
结束如果
intType = IIf(intPos = 0,intType,
Parameter.File)
ReadParameter =新参数
ReadParameter.Name = strName
ReadParameter。 Value = strValue
ReadParameter.Type = intType
ReadResponse.Add(ReadParameter)
结束如果
结束如果
intTagPos + = 1
结束如果
结束时
否则
抛出新的System.Exception(未找到表格。)
结束如果
结束功能
结束班级
公共类参数
继承System.Collections.CollectionBase
Public Sub Add(ByVal sFld As Parameter)
List.Add(sFld)
End Sub
Public ReadOnly Property Item(ByVal index As Integer)As
参数
获取
返回CType(List.Item(index),参数)
结束获取
结束物业
结束班级
公共类参数
公共Const输入为整数= 0
公共Const文件为整数= 1
Dim strName为字符串
Dim strValue As String
Dim intType as Integer
公共属性名称()字符串
获取
返回strName
结束获取
设置(ByVal vName为字符串)
strName = vName
结束集>
结束物业
公共财产价值()作为字符串
获取
返回strValue
结束获取
设置(ByVal vName为字符串)
strValue = vName
结束集
结束物业
公共物业类型()为整数
获取
返回intType
结束获取
设置(ByVal vType为整数)
intType = vType
结束集
结束财产
结束课程
公共课程条件
私人strName为字符串
私人strID为字符串
Public Sub New()
strName =""
strID =""
End Sub
Public Sub New(ByVal Name As String,ByVal ID As String)
strName = Name
strID = ID
结束分
Public Proper ty Name()As String
获取
返回strName
结束获取
Set(ByVal sValue As String)
strName = sValue
结束集
结束财产
公共财产ItemData ()作为字符串
获取
返回strID
结束获取
Set(ByVal iValue As字符串)
strID = iValue
结束集
结束属性
公共覆盖函数ToString( )作为字符串
返回strName
结束功能
结束班级
- 结束代码------ -----------------------------------------------
这里是为多部分表单生成的请求字符串:
- 启动requeststring ------------------ ------------------------
------------------ ----------- 7d4285126106b0
Content-Disposition:form-data; name =" condition"
830
----------------------- ------ 7d4285126106b0
Content-Disposition:form-data; name =" notes"
good good
---------------------- ------- 7d4285126106b0
Content-Disposition:form-data;命名= QUOT; IMAGE_FILE英寸; filename =""
内容类型:application / octet-stream
------------------- ---------- 7d4285126106b0
Content-Disposition:form-data; name =" image_url"
----------------------------- 7d4285126106b0
Content-Disposition:form-data; name =" version"
729
----------------------- ------ 7d4285126106b0
Content-Disposition:form-data; name =" domain_id"
1856
----------------------- ------ 7d4285126106b0
Content-Disposition:form-data; name =" meta_id"
1
----------------------- ------ 7d4285126106b0
Content-Disposition:form-data; name =" context_name"
w13.1097912464.0000318444
--------------------- -------- 7d4285126106b0
Content-Disposition:form-data; name =" x"
20
----------------------- ------ 7d4285126106b0
Content-Disposition:form-data; name =" y"
20
----------------------- ------ 7d4285126106b0--
- 结束requeststring ------------------------------ --------------
requeststring对我来说很合适。我想知道是否有一个
标题我应该设置我不见了。
感谢您的帮助。
-
格雷格
----
greg -at- spencersoft -dot - com
Okay. I''m getting "The remote server returned an error: (500)
internal server error".
I''ve got a form with two text boxes in the upper left corner,
txtHalfID and txtHalfPassword. There are three text boxes in the
upper right corner, txtISBN, txtDescription and txtPrice. There''s
also a drop down combo in the upper right, cmbCondition.
The bottom portion of the screen has a read only multi line text
box called txtResponse, where the raw HTML from the server is
displayed.
Below the left text boxes is a button, btnSignOn. If you click
that button, it will sign you onto Half.com using the ID and
password in the textboxes above it. This part works fine for me.
I can see the "Welcome to eBay" page in txtResponse.
Below the right boxes is a button called btnListBook. When I
click on it it should call four web pages on half.com in order.
First it calls http://half.ebay.com/help/sell_books.cfm. It fills
in the field in the form there with txtISBN.
Next it calls http://half.ebay.com/cat/sell/pmsearch.cgi. It
fills out the form there with txtDescription and cmbCondition.
This form allows file uploads, so it''s a multipart form. Although
my program''s output looks good to me, I get that server error
when I submit this form.
Here''s the code:
- Start Code ---------------------------------------------------
Imports System.Net
Public Class SBM
Inherits System.Windows.Forms.Form
Dim cc As New CookieCollection
Const encURL As Integer = 0
Const encMulti As Integer = 1
Private Sub SBM_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
txtHalfID.Text = ""
txtHalfPassword.Text = ""
txtISBN.Text = "0764560255"
txtDescription.Text = "good good"
txtPrice.Text = "99.99"
With cmbCondition
.Items.Add(New Conditions("Brand New", "830"))
.Items.Add(New Conditions("Like New", "840"))
.Items.Add(New Conditions("Very Good", "849"))
.Items.Add(New Conditions("Good", "859"))
.Items.Add(New Conditions("Acceptable", "864"))
.SelectedIndex = 0
End With
cmbCondition.SelectedIndex = 0
End Sub
Private Sub btnSignOn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSignOn.Click
Dim HWRequest As HttpWebRequest
Dim strURL As String
Dim HWParameters As Parameters
Dim intFound As Integer
HWRequest = GetRequest
("https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&UsingSSL=1
&co_partnerid=2&siteid=20")
strURL = "https://signin.half.ebay.com/ws/eBayISAPI.dll"
HWParameters = ReadResponse(HWRequest, strURL)
intFound = 0
For Each p As Parameter In HWParameters
Select Case p.Name
Case "userid"
p.Value = txtHalfID.Text
intFound += 1
Case "pass"
p.Value = txtHalfPassword.Text
intFound += 1
End Select
If intFound = 2 Then
Exit For
End If
Next
HWRequest = PostRequest(strURL, HWParameters, encURL)
GetResponse(HWRequest)
End Sub
Private Sub btnListBook_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnListBook.Click
Dim strPostData As String
Dim HWRequest As HttpWebRequest
Dim strURL As String
Dim HWParameters As Parameters
Dim intFound As Integer
Dim pr As Parameter
Try
HWRequest = GetRequest
("http://half.ebay.com/help/sell_books.cfm")
strURL = "http://half.ebay.com/cat/sell/pmsearch.cgi"
HWParameters = ReadResponse(HWRequest, strURL)
intFound = 0
For Each p As Parameter In HWParameters
Select Case p.Name
Case "p_code"
p.Value = txtISBN.Text
intFound += 1
End Select
If intFound = 1 Then
Exit For
End If
Next
HWRequest = PostRequest(strURL, HWParameters, encURL)
strURL = "/cat/sell/save_new_listing.cgi"
HWParameters = ReadResponse(HWRequest, strURL)
pr = New Parameter
pr.Name = "x"
pr.Value = 20
pr.Type = Parameter.Input
HWParameters.Add(pr)
pr = New Parameter
pr.Name = "y"
pr.Value = 20
pr.Type = Parameter.Input
HWParameters.Add(pr)
intFound = 0
For Each p As Parameter In HWParameters
Select Case p.Name
Case "notes"
p.Value = txtDescription.Text
intFound += 1
Case "condition"
p.Value = cmbCondition.Items
(cmbCondition.SelectedIndex).ItemData
intFound += 1
End Select
If intFound = 2 Then
Exit For
End If
Next
strURL =
"http://half.ebay.com/cat/sell/save_new_listing.cgi"
HWRequest = PostRequest(strURL, HWParameters,
encMulti)
strURL = "sell.jsp"
HWRequest.Referer =
"http://half.ebay.com/cat/sell/pmsearch.cgi"
''the internal server error is generated in this call
''to ReadResponse
HWParameters = ReadResponse(HWRequest, strURL)
intFound = 0
For Each p As Parameter In HWParameters
Select Case p.Name
Case "itemPrice"
p.Value = txtPrice.Text
intFound += 1
End Select
If intFound = 1 Then
Exit For
End If
Next
strURL = "http://half.ebay.com/cat/sell/sell.jsp"
HWRequest = PostRequest(strURL, HWParameters, encURL)
GetResponse(HWRequest)
MessageBox.Show("Success!", MsgBoxStyle.OKOnly, "Book
Posted")
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error!!!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MsgBox("Half.com appears to have changed its posting
procedures. As a result, this version of SBM cannot list books on
it.", MsgBoxStyle.Exclamation, "Half.com Listing Problem")
End Try
End Sub
Private Function GetRequest(ByVal strURL As String) As
HttpWebRequest
GetRequest = CreateRequest(strURL)
GetRequest.Method = "GET"
End Function
Private Function PostRequest(ByVal strURL As String, ByVal
PostParameters As Parameters, ByVal intType As Integer) As
HttpWebRequest
Dim encoding As New System.Text.ASCIIEncoding
Dim byte1 As Byte()
Dim newStream As System.IO.Stream
Dim strData As String
Const strBoundary As String =
"---------------------------7d4285126106b0"
PostRequest = CreateRequest(strURL)
PostRequest.Method = "POST"
If intType = encURL Then
PostRequest.ContentType = "application/x-www-form-
urlencoded"
Else
PostRequest.ContentType = "multipart/form-data,
boundary=" & strBoundary
End If
strData = BuildRequestString(PostParameters, intType,
strBoundary)
PostRequest.ContentLength = strData.Length
byte1 = encoding.GetBytes(strData)
newStream = PostRequest.GetRequestStream
newStream.Write(byte1, 0, byte1.Length)
newStream.Close()
End Function
Private Function BuildRequestString(ByVal RequestParameters
As Parameters, ByVal intType As Integer, ByVal strBoundary As
String)
BuildRequestString = ""
Select Case intType
Case encURL
For Each p As Parameter In RequestParameters
BuildRequestString &= p.Name.Trim & "=" &
p.Value.Trim & "&"
Next
BuildRequestString = Mid(BuildRequestString, 1,
Len(BuildRequestString) - 1)
Case encMulti
''the requeststring generated for the multipart form is
''shown below
For Each p As Parameter In RequestParameters
BuildRequestString &= "--" & strBoundary &
vbCrLf & "Content-Disposition: form-data; name=""" & p.Name.Trim
& """"
Select Case p.Type
Case Parameter.File
BuildRequestString &= ";
filename=""""" & vbCrLf & "Content-Type: application/octet-
stream" & vbCrLf & vbCrLf & vbCrLf
Case Parameter.Input
BuildRequestString &= vbCrLf & vbCrLf
& p.Value & vbCrLf
End Select
Next
BuildRequestString &= "--" & strBoundary & "--"
End Select
End Function
Private Function CreateRequest(ByVal strURL As String) As
HttpWebRequest
Dim Uri As Uri
Uri = New Uri(strURL)
CreateRequest = HttpWebRequest.Create(Uri)
CreateRequest.AllowAutoRedirect = True
CreateRequest.CookieContainer = New CookieContainer
If cc.Count > 0 Then
CreateRequest.CookieContainer.Add(cc)
End If
End Function
Private Sub GetResponse(ByVal ReadRequest As HttpWebRequest)
Dim HWResponse As HttpWebResponse
Dim cookie As Cookie
HWResponse = ReadRequest.GetResponse()
If HWResponse.Cookies.Count > 0 Then
For Each cookie In HWResponse.Cookies
cc.Add(cookie)
Next
End If
''debugging
Dim sr As System.IO.StreamReader
Dim strResult As String
sr = New System.IO.StreamReader
(HWResponse.GetResponseStream())
txtResponse.Text = sr.ReadToEnd
sr.Close()
''debugging
End Sub
Private Function ReadResponse(ByVal ReadRequest As
HttpWebRequest, ByVal ReadURL As String) As Parameters
Dim HWResponse As HttpWebResponse
Dim cookie As Cookie
Dim sr As System.IO.StreamReader
Dim strResult As String
Dim intTagPos As Integer
Dim intLength As Integer
Dim intURLPos As Integer
Dim strTag As String
Dim blnFormFound As Boolean
Dim blnMoreForms As Boolean
Dim intPos As Integer
Dim strName As String
Dim strValue As String
Dim ReadParameter As Parameter
Dim intType As Integer
Dim intInputPos As Integer
Dim intTextPos As Integer
Dim intSelectPos As Integer
Dim blnTagFound As Boolean
ReadResponse = New Parameters
''the next line generates the internal server error
HWResponse = ReadRequest.GetResponse()
If HWResponse.Cookies.Count > 0 Then
For Each cookie In HWResponse.Cookies
cc.Add(cookie)
Next
End If
sr = New System.IO.StreamReader
(HWResponse.GetResponseStream())
strResult = sr.ReadToEnd
''debugging
txtResponse.Text = strResult
''debugging
blnFormFound = False
blnMoreForms = True
intTagPos = 1
While Not blnFormFound And blnMoreForms
intTagPos = InStr(intTagPos, strResult.ToUpper,
"<FORM", CompareMethod.Text)
If intTagPos <> 0 Then
intLength = InStr(intTagPos, strResult, ">",
CompareMethod.Text) - intTagPos
strTag = strResult.Substring(intTagPos,
intLength)
intURLPos = InStr(1, strTag.ToUpper,
ReadURL.ToUpper, CompareMethod.Text)
If intURLPos <> 0 Then
intLength = InStr(intTagPos,
strResult.ToUpper, "</FORM>", CompareMethod.Text) - intTagPos
strResult = strResult.Substring(intTagPos,
intLength)
blnFormFound = True
Else
intTagPos += 1
End If
Else
blnMoreForms = False
End If
End While
If blnFormFound Then
intTagPos = 1
While intTagPos <> 0
intInputPos = InStr(intTagPos, strResult.ToUpper,
"<INPUT", CompareMethod.Text)
intTextPos = InStr(intTagPos, strResult.ToUpper,
"<TEXTAREA", CompareMethod.Text)
intSelectPos = InStr(intTagPos,
strResult.ToUpper, "<SELECT", CompareMethod.Text)
intTagPos = IIf(intTextPos <> 0 And intTextPos <
intInputPos, intTextPos, intInputPos)
intTagPos = IIf(intSelectPos <> 0 And
intSelectPos < intTagPos, intSelectPos, intTagPos)
intType = Parameter.Input
If intTagPos <> 0 Then
intLength = InStr(intTagPos, strResult, ">",
CompareMethod.Text) - intTagPos
strTag = strResult.Substring(intTagPos,
intLength)
intPos = InStr(1, strTag.ToUpper,
"TYPE=SUBMIT", CompareMethod.Text)
If intPos = 0 Then
intPos = InStr(1, strTag.ToUpper,
"TYPE=""SUBMIT""", CompareMethod.Text)
End If
If intPos = 0 Then
intPos = InStr(1, strTag.ToUpper,
"NAME=""", CompareMethod.Text)
If intPos = 0 Then
intPos = InStr(1, strTag.ToUpper,
"NAME=", CompareMethod.Text)
intLength = InStr(intPos + 5, strTag,
" ", CompareMethod.Text) - intPos - 5
strName = strTag.Substring(intPos +
4, intLength)
Else
intLength = InStr(intPos + 6, strTag,
"""", CompareMethod.Text) - intPos - 6
strName = strTag.Substring(intPos +
5, intLength)
End If
If intPos <> 0 Then
intPos = InStr(1, strTag.ToUpper,
"VALUE=""", CompareMethod.Text)
If intPos = 0 Then
strValue = ""
Else
intLength = InStr(intPos + 7,
strTag, """", CompareMethod.Text) - intPos - 7
strValue = strTag.Substring
(intPos + 6, intLength)
End If
intPos = InStr(1, strTag.ToUpper,
"TYPE=""FILE""", CompareMethod.Text)
If intPos = 0 Then
intPos = InStr(1, strTag.ToUpper,
"TYPE=FILE", CompareMethod.Text)
End If
intType = IIf(intPos = 0, intType,
Parameter.File)
ReadParameter = New Parameter
ReadParameter.Name = strName
ReadParameter.Value = strValue
ReadParameter.Type = intType
ReadResponse.Add(ReadParameter)
End If
End If
intTagPos += 1
End If
End While
Else
Throw New System.Exception("Form not found.")
End If
End Function
End Class
Public Class Parameters
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal sFld As Parameter)
List.Add(sFld)
End Sub
Public ReadOnly Property Item(ByVal index As Integer) As
Parameter
Get
Return CType(List.Item(index), Parameter)
End Get
End Property
End Class
Public Class Parameter
Public Const Input As Integer = 0
Public Const File As Integer = 1
Dim strName As String
Dim strValue As String
Dim intType As Integer
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal vName As String)
strName = vName
End Set
End Property
Public Property Value() As String
Get
Return strValue
End Get
Set(ByVal vName As String)
strValue = vName
End Set
End Property
Public Property Type() As Integer
Get
Return intType
End Get
Set(ByVal vType As Integer)
intType = vType
End Set
End Property
End Class
Public Class Conditions
Private strName As String
Private strID As String
Public Sub New()
strName = ""
strID = ""
End Sub
Public Sub New(ByVal Name As String, ByVal ID As String)
strName = Name
strID = ID
End Sub
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal sValue As String)
strName = sValue
End Set
End Property
Public Property ItemData() As String
Get
Return strID
End Get
Set(ByVal iValue As String)
strID = iValue
End Set
End Property
Public Overrides Function ToString() As String
Return strName
End Function
End Class
- End Code -----------------------------------------------------
Here''s the requeststring generated for the multipart form:
- Start requeststring ------------------------------------------
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="condition"
830
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="notes"
good good
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="image_file"; filename=""
Content-Type: application/octet-stream
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="image_url"
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="version"
729
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="domain_id"
1856
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="meta_id"
1
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="context_name"
w13.1097912464.0000318444
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="x"
20
-----------------------------7d4285126106b0
Content-Disposition: form-data; name="y"
20
-----------------------------7d4285126106b0--
- End requeststring --------------------------------------------
The requeststring looks right to me. I''m wondering if there''s a
header I should set that I''m missing.
Thanks for the help.
--
Greg
----
greg -at- spencersoft -dot- com
这篇关于使用Httpwebrequest提交multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!