如何检测WebRequest由于Web代理错误而不是目标Web服务器错误而失败?
try
{
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com");
request.Proxy = new WebProxy("localhost");
var response = request.GetResponse();
return response.GetResponseStream();
}
catch(WebException webex)
{
//Detect proxy failure
}
Run Code Online (Sandbox Code Playgroud) c# proxy httpwebrequest httpwebresponse system.net.webexception
是否可以从System.Net.HttpWebResponse读取图像附件?
我有一个url到一个生成图像的java页面.
当我在firefox中打开url时,会出现下载对话框.内容类型是application/png.似乎工作.
当我在c#中尝试这个并发出GET请求时,我检索内容类型:text/html并且没有内容处置标头.
简单代码:
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(uri); HttpWebResponse response =(HttpWebResponse)request.GetResponse();
"response.GetResponseStream()"为空.
尝试使用java是成功的.
我是否必须准备webrequest或其他东西?
我正在尝试从我的.net应用程序登录Web应用程序,但由于某种原因它无法正常工作.这是登录代码:
<form action="./process-login.php" method="post">
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td>Username:</td>
<td><input type="text" size="20" name="username" value=""></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" size="20" name="password" value=""></td>
</tr>
<tr>
<td><input type="submit" name="axn" value=Login></td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
这是我如何从.net做到的:
string userName = "user";
string password = "password";
string postData = "username=" + userName;
postData += ("&password=" + password);
postData += ("&axn=Login");
HttpWebRequest loginRequest = (HttpWebRequest)
WebRequest.Create("http://server.com/process-login.php");
//Added following answer begin
CookieContainer CC = new CookieContainer();
loginRequest.CookieContainer = CC;
//Added following answer end
loginRequest.Method = "POST";
loginRequest.Accept …Run Code Online (Sandbox Code Playgroud) 我试图获取标题"Set-Cookie"或访问cookie容器,但Set-Cookie标头不可用.cookie位于响应头中,但它不在客户端请求对象中.我正在ClientHttp使用注册堆栈
bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
Run Code Online (Sandbox Code Playgroud)
这是回复:
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Connection: keep-alive
Status: 200
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0.pre4
ETag: "39030a9c5a45a24e485e4d2fb06c6389"
Client-Version: 312, 105, 0, 0
X-Runtime: 44
Content-Length: 1232
Set-Cookie: _CWFServer_session=[This is the session data]; path=/; HttpOnly
Cache-Control: private, max-age=0, must-revalidate
Server: nginx/0.7.67 + Phusion Passenger 3.0.0.pre4 (mod_rails/mod_rack)
<?xml version="1.0" encoding="UTF-8"?>
<user>
...
</user>
Run Code Online (Sandbox Code Playgroud)
我的回调代码包含如下内容:
var webRequest = (HttpWebRequest)result.AsyncState;
raw = webRequest.EndGetResponse(result) as HttpWebResponse;
foreach (Cookie c in webRequest.CookieContainer.GetCookies(webRequest.RequestUri))
{
Console.WriteLine("Cookie['" + c.Name + …Run Code Online (Sandbox Code Playgroud) 我已经尝试了一切,我无法弄清楚为什么会发生这种错误.
背景:我有一个用MonoTouch编写的IPad应用程序,我有一个在后台运行的线程,每隔15秒我就会与服务器同步数据.这适用于线程的前几次迭代,但最终我获得了以下堆栈跟踪.
An exception occured: System.Net.WebException: Error getting response stream (ReadDone4): ServerProtocolViolation ---> System.FormatException: Input string was not in the correct format
at System.UInt32.Parse (System.String s) [0x00010] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/UInt32.cs:405
at System.Net.WebConnection.GetResponse (System.Byte[] buffer, Int32 max) [0x000ba] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:565
at System.Net.WebConnection.ReadDone (IAsyncResult result) [0x00095] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:446
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:819
at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:827
at SyncService.REST.RestClient.Execute[IEnumerable`1] (SyncService.REST.RestRequest request) [0x00079] in /Users/Chris/Compass/SyncService/REST/RestClient.cs:42
Run Code Online (Sandbox Code Playgroud)
我正在使用默认配置的IIS网络服务器.这是我打电话的方法:
public RestResponse<T> Execute<T>(RestRequest …Run Code Online (Sandbox Code Playgroud) 虽然我发现了许多关于如何使用HttpWebRequest和Response进行GET和POST的文章和其他信息,但我发现自己很难让事情发挥作用,就像我期望它们一样.
我一直在玩我发现的几个想法,但到目前为止,没有任何工作......我会发布我的代码:
private void start_post()
{
string username = txtUser.Text;
string password = txtPassword.Text;
string strResponce;
byte[] buffer = Encoding.ASCII.GetBytes("username="+username+"&password="+password);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
WebReq.Method = "POST";
//WebReq.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
WebReq.Headers.Add("Translate", "F");
WebReq.AllowAutoRedirect = true;
WebReq.CookieContainer = cookieJar;
WebReq.KeepAlive = true;
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//txtResult.Text …Run Code Online (Sandbox Code Playgroud) 我正在发送1个httpWebRequest并阅读响应.我在回复中获得了整页.我想获得1个div,这是来自响应的名称ad rate.那么我该如何匹配这种模式呢?
我的代码是这样的:
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://www.domain.com/");
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream response = WebResp.GetResponseStream();
StreamReader data = new StreamReader(response);
string result = data.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
我得到的反应如下:
<HTML><BODY><div id="rate">Todays rate 55 Rs.</div></BODY></HTML>
Run Code Online (Sandbox Code Playgroud)
我想读取div率的数据.即我应该获得内容"今天率55卢比."
那我怎么能为这个做正则表达式???
我在C#中有以下代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000;
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
Stream newStream = request.GetRequestStream();
newStream.Write(bPostData, 0, bPostData.Length);
byte[] buf = new byte[1025]; int read = 0; string sResp = "";
HttpWebResponse wResp = (HttpWebResponse)request.GetResponse();
Stream resp = wResp.GetResponseStream();
Run Code Online (Sandbox Code Playgroud)
该行HttpWebResponse wResp =...只是挂起(因为URL没有响应)。我不确定它到底在哪里崩溃(因为我什至没有得到异常错误)。我在IE中测试了URL,它工作正常。我还检查了bPostData,并且其中包含数据。哪里出了问题?
我现在很漂亮,因为我不断收到亚马逊的不完整回复.我正在使用Product Advertising API,向服务器发出一个ItemLookup请求.
C#代码非常基本:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string resultString;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
resultString = sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
我收到的字符数是17408-非常稳定,但我也看到了16k左右的东西.
这就是它总是如何结束:
...ount><CurrencyCode>EUR</CurrencyCode><FormattedPrice>EUR 11,33</FormattedPri
Run Code Online (Sandbox Code Playgroud)
有什么我不知道HttpWebRequest或亚马逊的API?请帮忙!
网址(不关心密钥)编辑:坏主意,限制超出...
我正在开发一个项目,要求从某个站点跟踪请求"ajax ones",访问其中一些请求的响应有效负载并对其进行操作.
到目前为止,我设法使用webRequest api跟踪请求并访问其标头,唯一的问题是我无论如何都无法访问这些响应中的实际数据.
它甚至可能吗?
请随时发布任何有用的想法或参考资料.
感谢,并有一个愉快的一天.
编辑:
我们正在寻找的一个例子是网络面板中的响应选项卡,它是chrome开发人员工具的一部分.
google-chrome httpwebresponse google-chrome-extension google-chrome-devtools
httpwebresponse ×10
c# ×9
amazon ×1
asp.net ×1
attachment ×1
cookies ×1
login ×1
mono ×1
proxy ×1
regex ×1
response ×1
xamarin.ios ×1