我知道这有点像重新发明轮子,但我需要知道通过http/soap/xml和Web消息与Web服务进行通信.原因是我需要与第三方Web服务进行通信以进行工作,但是WSDL或其他东西有问题,并且在使用.NET向导连接到它时它不起作用.
所以,任何人都可以给我一个过程/简单的例子/等.怎么做或任何人都可以给我一个解释它的地方的链接?我对网络请求和响应并不十分了解.
如何构建和发送请求?我如何解析响应?
这是一个简单的Web服务的代码.假设.asmx的地址是"http://www.mwebb.com/TestSimpleService.asmx":
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace TestSimpleService
{
[WebService]
public class Soap : System.Web.Services.WebService
{
[WebMethod]
public string SayHello(string name)
{
return "Hello " + name + "!";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼这种方法?
任何帮助表示赞赏.
编辑
我真的只想知道如何将数据发送到Web服务.我可以获取所有方法/ SOAP操作/ URL数据,我可以解析响应数据.我只是不知道使用什么对象或如何使用它们.
如果有人知道一些简单的.NET SOAP客户端,比如Python中的SUDS,那也会有所帮助.
我最近发现以下内容不适用于某些网站,例如IMDB.com.
class Program
{
static void Main(string[] args)
{
try
{
System.Net.WebRequest wc = System.Net.WebRequest.Create("http://www.imdb.com"); //args[0]);
((HttpWebRequest)wc).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.1 Safari/525.19";
wc.Timeout = 1000;
wc.Method = "HEAD";
WebResponse res = wc.GetResponse();
var streamReader = new System.IO.StreamReader(res.GetResponseStream());
Console.WriteLine(streamReader.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它返回HTTP 405(Method Not Allowed).我的问题是,我使用与上面非常相似的代码来检查链接是否有效以及绝大多数时候它是否正常工作.我可以将它切换到等于GET的方法并且它可以工作(增加超时),但这会使事情减慢一个数量级.我假设405响应是IMDB服务器端的服务器配置.
有没有办法让我在.NET中以轻量级的方式做同样的事情?或者,有没有办法修复上面的代码,所以它作为一个与imdb一起使用的GET请求?
我有一个网站几个月来一直工作正常.今天早上我开始收到503 Service Unavailable错误.检查IIS后,我注意到应用程序池正在停止.由于我之前遇到过问题,我怀疑WebRequest是问题所在.所以我注释掉了网站的WebRequest部分,猜猜是什么,应用程序池不再被禁用.我怀疑还有另一个安全更新,我无法弄清楚我还需要做些什么来让WebRequest再次运行.
我已经尝试过的事情:
1)aspnet_regiis -u然后-i
2)重新安装.net框架
更多信息:我使用Windows认证的asp.net v4.0应用程序池的NetworkService帐户
事件日志中的错误是:
"HipIISEngineStub.dll无法加载.数据是错误."
var request = (HttpWebRequest)WebRequest.Create(path1);
request.Credentials = CredentialCache.DefaultCredentials;
request.PreAuthenticate = true;
var getResponse = new Func<WebRequest, WebResponse>(req => req.GetResponse());
try
{
return getResponse(request).GetResponseStream();
}
Run Code Online (Sandbox Code Playgroud) 当我使用Chrome API收听所有HTTP请求时,如何从中获取实际数据?
我的意思是如果请求是在php页面(XMLHttpRequest)上进行的,我该如何获取此页面的内容?
.
我现在正在使用ajax请求查询数据.但这不是一个好的解决方案.
主要问题是请求使用POST方法.从ajax查询收到的数据与从HttpRequest收到的数据不同.
javascript webrequest httpwebrequest google-chrome-extension
我是新来的C#小号await/async,目前玩弄了一下.
在我的场景中,我有一个简单的客户端对象,它有一个WebRequest属性.客户端应该通过WebRequests 定期发送活动消息RequestStream.这是client-object的构造函数:
public Client()
{
_webRequest = WebRequest.Create("some url");
_webRequest.Method = "POST";
IsRunning = true;
// --> how to start the 'async' method (see below)
}
Run Code Online (Sandbox Code Playgroud)
和async alive-sender方法
private async void SendAliveMessageAsync()
{
const string keepAliveMessage = "{\"message\": {\"type\": \"keepalive\"}}";
var seconds = 0;
while (IsRunning)
{
if (seconds % 10 == 0)
{
await new StreamWriter(_webRequest.GetRequestStream()).WriteLineAsync(keepAliveMessage);
}
await Task.Delay(1000);
seconds++;
}
}
Run Code Online (Sandbox Code Playgroud)
该方法应该如何开始?
new Thread(SendAliveMessageAsync).Start();
要么
Task.Run(SendAliveMessageAsync); //将返回类型更改为Task …
我一直在尝试/使用以下命令从powershell 访问带有字符的URL (它是对gitlab服务器的查询以检索调用的项目"foo/bar"):
Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
Run Code Online (Sandbox Code Playgroud)
现在,奇怪的是使用PowerShell ISE或Visual Studio,请求是正常的.使用PowerShell本身时,URL会自动取消转义,并且请求失败.例如
在ISE/VS中:
$> Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
VERBOSE: GET https://server.com/api/v3/projects/foo%2Fbar with 0-byte payload
VERBOSE: received 19903-byte response of content type application/json
StatusCode : 200
StatusDescription : OK
Content : .... data ....
Run Code Online (Sandbox Code Playgroud)
在Powershell中:
$> Invoke-WebRequest https://server.com/api/v3/projects/foo%2Fbar -Verbose
VERBOSE: GET https://server.com/api/v3/projects/foo/bar with 0-byte payload
Invoke-WebRequest : {"error":"404 Not Found"}
At line:1 char:1
+ Invoke-WebRequest 'https://server.com/api/v3/projects/foo%2Fbar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)
我尝试在URL周围添加单引号和双引号,但没有任何帮助. …
我正在尝试使用HttpWebRequest和HttpWebResponse从网页中检索HTML代码.
response = (HttpWebResponse)request.GetResponse();
...
Stream stream = response.GetResponseStream();
Run Code Online (Sandbox Code Playgroud)
响应对象的ContentLength值为106142.当我查看流对象时,它的长度为65536.使用ReadToEnd()使用StreamReader读取流时,仅返回前65536个字符.
我怎样才能获得整个代码?
编辑:
使用以下代码段:
catch (WebException ex)
{
errorMessage = errorMessage + ex.Message;
if (ex.Response != null) {
if (ex.Response.ContentLength > 0)
{
using (Stream stream = ex.Response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string pageOutput = reader.ReadToEnd().Trim();
Run Code Online (Sandbox Code Playgroud)
ex.Response.ContentLength = 106142
ex.Response.GetResponseStream().长度= 65536
stream.Length = 65536
pageOutput.Length = 65534(由于修剪)
是的,代码实际上是截断的.
如果我有以下URL:
http://test.com?x=1&x=2&x=3&x=4&x=5&x=6&x=7
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能读出所有"x"值?
添加了新评论:感谢您的所有答案.我基本上来自Java和.Net背景,最近开始寻找Ruby和Rails.就像在Java中一样,我们没有像request.getParameterValues("x")那样的东西;
我下载了一些文件,但我也想为webclient设置超时.我看到没有变化只是我们可以使用重写WebRequest.我已经做了但它不起作用.我的意思是重写GetWebRequest方法不起作用..这是我的代码
public class VideoDownloader : Downloader
{
/// <summary>
/// Initializes a new instance of the <see cref="VideoDownloader"/> class.
/// </summary>
/// <param name="video">The video to download.</param>
/// <param name="savePath">The path to save the video.</param>
public VideoDownloader(VideoInfo video, string savePath)
: base(video, savePath)
{ }
/// <summary>
/// Starts the video download.
/// </summary>
public override void Execute()
{
// We need a handle to keep the method synchronously
var handle = new ManualResetEvent(false);
var client = new WebClient();
client.DownloadFileCompleted …Run Code Online (Sandbox Code Playgroud) 我正在尝试在FTP服务器上创建一个文件,但我所拥有的只是一个字符串或数据流以及应该用它创建的文件名.有没有办法在流或字符串上创建服务器上的文件(我没有创建本地文件的权限)?
string location = "ftp://xxx.xxx.xxx.xxx:21/TestLocation/Test.csv";
WebRequest ftpRequest = WebRequest.Create(location);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Credentials = new NetworkCredential(userName, password);
string data = csv.getData();
MemoryStream stream = csv.getStream();
//Magic
using (var response = (FtpWebResponse)ftpRequest.GetResponse()) { }
Run Code Online (Sandbox Code Playgroud) webrequest ×10
c# ×6
.net ×4
asp.net ×1
async-await ×1
asynchronous ×1
ftp ×1
javascript ×1
pool ×1
powershell ×1
ruby ×1
timeout ×1
url ×1
urlencode ×1
web-services ×1
webclient ×1