如何发送数据以验证服务器?使用当前代码:
WebClient MyWebClient = new WebClient();
MyWebClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(MyWebClient_OpenWriteCompleted);
MyWebClient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; ICS)";
MyWebClient.OpenWriteAsync(new Uri("http://myserver.com/login"), "POST", "email=myemail@server.com&pass=mypassword");
void MyWebClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
throw new NotImplementedException();
}
在此代码之后,如果程序嗅探器(HttpAnalyzer)查看Headers和Response Content,则在响应内容中将写入:Request is not completed. waiting ...
,响应标头将为空...
如何发表帖子请求?
答案 0 :(得分:1)
根据doc,当您关闭在OpenWriteCompleted
事件的参数中传递给您的流时,实际上会发送内容。
由于您没有正确关闭流,因此不会发送任何内容。
答案 1 :(得分:0)
//Making a POST request using WebClient.
Function()
{
WebClient wc = new WebClient();
var URI = new Uri("http://your_uri_goes_here");
//如果需要任何编码。
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
//或任何其他编码类型。
//如果需要任何密钥
wc.Headers["KEY"] = "Your_Key_Goes_Here";
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.UploadStringAsync(URI,"POST","Data_To_Be_sent");
}
void wc__UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
MessageBox.Show(e.Result);
// e.result会根据您的POST请求获取响应。
}
catch(Exception exc)
{
MessageBox.Show(exc.ToString());
}
}