所以我正试图将一些东西发布到网络服务器上.
System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("url");
System.String Content = "id=" + Id;
EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content);
EventReq.Method = "POST";
EventReq.ContentType = "application/x-www-form-urlencoded";
System.IO.StreamWriter sw = new System.IO.StreamWriter(EventReq.GetRequestStream(), System.Text.Encoding.UTF8);
sw.Write(Content);
sw.Flush();
sw.Close();
Run Code Online (Sandbox Code Playgroud)
看起来没问题,我根据ENCODED数据的大小设置内容长度...无论如何,它在sw.flush()失败,"要写入流的字节超过指定的内容长度大小"
StreamWriter在我背后做了一些魔法,我不知道吗?有没有办法让我了解StreamWriter正在做什么?
我有一个winforms应用程序,我一直在努力对消费者帐户进行多项测试.测试需要一次登录才能执行.
string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
requestLogin.Method = "POST";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
Run Code Online (Sandbox Code Playgroud)
这个脚本适用于登录就好了,问题是当我需要实际运行测试时我需要将所有结果都返回到一个字符串中(HTML结果).
private string runTestRequest(Uri url, string parameters)
{
string testResults = string.Empty;
HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
runTest.CookieContainer = cookieJar;
runTest.Method = "POST";
runTest.ContentType = "application/x-www-form-urlencoded";
StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), …Run Code Online (Sandbox Code Playgroud) 我希望实现以下内容:
UrlBuilder ub = new UrlBuilder("http://www.google.com/search");
ub.Parameters.Add("q","request");
ub.Parameters.Add("sourceid","ie8");
string uri = ub.ToString(); //http://www.google.com/search?q=request&sourceid=ie8
Run Code Online (Sandbox Code Playgroud)
.NET中有什么东西,或者我必须创建自己的东西吗?
我试图使用httpwebrequest在远程服务器上使用类似服务的休息,并且从第一次执行本身开始,我的代码挂起了程序.然后我尝试将它作为控制台应用程序,以确保它与程序本身无关,但没有运气!
string credentialsJson = @"{""username"":""test"",
""password"":""test""
}";
int tmp = ServicePointManager.DefaultConnectionLimit;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://qrua.com/qr/service" + @"/auth/login");
request.Method = "POST";
request.KeepAlive = true;
request.Timeout = 50000 ;
request.CookieContainer = new CookieContainer();
request.ContentType = "application/json";
try
{
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(credentialsJson);
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION:" + e.Message);
}
//WebResponse response = request.GetResponse();
try
{
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("request:\n" + request.ToString() + "\nresponse:\n" + response.ContentLength);
response.Close();
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION: in sending …Run Code Online (Sandbox Code Playgroud) 我在德国的某家公司遇到了问题.他们在他们的网络中使用代理,我的程序无法与服务器通信.
IE使用此设置:

这意味着:自动检测设置
这是代码:
public static bool CompleteValidation(string regKey)
{
string uri = "***";
int c = 1;
if (Counter < 5) c = 6 - Counter;
string response = "";
try
{
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "pl,en-us;q=0.7,en;q=0.3");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-2,utf-8;q=0.7,*;q=0.7");
request.KeepAlive = true;
//proxy settings
string exepath = Path.GetDirectoryName(Application.ExecutablePath);
string proxySettings = exepath + …Run Code Online (Sandbox Code Playgroud) 我从公共数据库URI http://data.seattle.gov/api/views/3k2p-39jp/rows.json导入JSON数据, 行直到445454.我使用以下代码构建JSON对象整个数据.
HttpGet get = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder=new StringBuilder();
for(String line=null;(line = reader.readLine()) != null;){
builder.append(line).append("\n");
}
JSONTokener jsonTokener=new JSONTokener(builder.toString());
JSONObject finalJson=new JSONObject(jsonTokener);
JSONArray data=finalJson.getJSONArray("data");
Run Code Online (Sandbox Code Playgroud)
因为数据太大,我正在03-21 03:41:49.714: E/AndroidRuntime(666): Caused by: java.lang.OutOfMemoryError指出错误的来源buildr.append(line).append("\n").无论如何,我可以处理大型数据集而不会出现内存分配问题吗?
我试图使用System.Web从服务器下载文件.它确实有效,但有些链接给我带来了麻烦.链接看起来像这样:
http://cdn.somesite.com/r1KH3Z%2FaMY6kLQ9Y4nVxYtlfrcewvKO9HLTCUBjU8IBAYnA3vzE1LGrkqMrR9Nh3jTMVFZzC7mxMBeNK5uY3nx5K0MjUaegM3crVpFNGk6a6TW6NJ3hnlvFuaugE65SQ4yM5754BM%2BLagqYvwvLAhG3DKU9SGUI54UAq3dwMDU%2BMl9lUO18hJF3OtzKiQfrC/the_file.ext
Run Code Online (Sandbox Code Playgroud)
代码看起来基本上是这样的:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
WebResponse response = request.getResponse();
Run Code Online (Sandbox Code Playgroud)
getResponse()始终抛出异常(错误400错误请求).但是,我知道该链接有效,因为我可以使用Firefox下载该文件而不会出现问题.
我也尝试用Uri.UnescapeDataString(链接)解码链接,但该链接甚至不能在Firefox中工作.
其他链接以这种方式完美地工作..只是这些不起作用.
好的,我发现了一些使用wireshark的东西:
如果我使用Firefox打开链接,则发送:
&ME3@"dM*PNyAo PA:]GET /r1KH3Z%2FaMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs4QgbudzcrJivrAaOTYkEnozqmdoSCCY8yb1i22YtEAV/epd_outpost_12adb.flv HTTP/1.1
Host: cdn.somesite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
我认为只有第一行是问题,因为WebRequest.Create(link)解码了url:
&MEz.@!dM/nP9@~P>.GET /r1KH3Z/aMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs6Mmh1EsQQ4vJVYUwtbLBDNx9AwCHlWDfzfSWIHzaaIo/epd_outpost_12adb.flv HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Host: cdn.somesite.com
Run Code Online (Sandbox Code Playgroud)
(%2F替换为/)
我发现Uri类自动解码url:Uri uri = new Uri(link); //链接未解码Debug.WriteLine(uri.ToString()); //链接在此解码.
我怎么能阻止这个?
在此先感谢您的帮助.
我有一个可移植类库,其目标是".NET for Windows Store应用程序"和"Windows Phone 7.5或更高版本".我发出HTTP POST请求,截至上周,负责后端的管理员决定我需要发送一个0的ContentLength而不是.NET默认的-1.我使用WebRequest类,但我足够灵活,可以根据需要使用HttpWebRequest.
通常我会使用WebRequest.Create并设置ContentLength属性.在PCL库中,ContentLength属性不可用.如果我尝试添加一个带有"Content-Length"键的Header,框架会抱怨我应该使用ContentLength属性.
关于如何在PCL中设置ContentLength的任何想法?
我试图拦截chrome扩展中的代理授权.在这里给出答案:Chrome扩展程序中的域授权和阅读文档,我的代码如下所示:
chrome.webRequest.onAuthRequired.addListener(
function(details, callbackFn) {
console.log("onAuthRequired!", details, callbackFn);
//callback({
// authCredentials: {username: "1", password: "__TestUse"}
//});
},
{urls: ["<all_urls>"]}
);
Run Code Online (Sandbox Code Playgroud)
问题是callbackFn 未定义但应该是一个函数.
任何人都有一些想法为什么callbackFn是未定义的.当我阅读文档时,我正在做对..
我正在使用Xamarin开发一个Android应用程序,在将Xamarin升级到它的最新版本之后,我的WebRequest中抛出了以下异常:
{System.Net.WebException:获取响应流时出错(ReadDone1):ReceiveFailure ---> System.IO.IOException:EndRead failure ---> System.Net.Sockets.SocketException:System.Net.Sockets上的peer重置连接.Socket.EndReceive(IAsyncResult结果)[0x00000] in:0在System.Net.Sockets.NetworkStream.EndRead(IAsyncResult ar)[0x00000] in:0 ---内部异常堆栈跟踪结束---在System.Net .Sockets.NetworkStream.EndRead(IAsyncResult ar)[0x00000] in:0 at Mono.Security.Protocol.Tls.SslStreamBase.InternalReadCallback(IAsyncResult result)[0x00000] in:0 ---内部异常堆栈跟踪结束---在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)[0x00000] in:0 in System.Threading.Tasks.TaskFactory1 [System.Net.WebResponse] .InnerInvoke(System.Threading.Tasks.TaskCompletionSource1 tcs,System.Func)
2 endMethod,IAsyncResult l)[0x00000] in:0 ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x00000] in:0 at System. Runtime.CompilerServices.TaskAwaiter 1 [System.Net.WebResponse] .GetResult()[0x00000] in:0 at modulo.CICC.Chat.Core.ConnectionFactory.WebServices + d _32.MoveNext()[0x000db] in c:\ Users\Raphael Alvarenga\Documents\Projects\SESGE\Modulo.CICC.Chat\Modulo.CICC.Chat.Core\ConnectionFactory\WebServices.cs:205}
这是代码:
try
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
string data = "access_token=" + System.Web.HttpUtility.UrlEncode(accessToken) + "&refresh_token=" + System.Web.HttpUtility.UrlEncode(refreshToken);
WebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url + "?" + data);
myWebRequest.Method = method;
myWebRequest.ContentLength = data.Length;
myWebRequest.ContentType …Run Code Online (Sandbox Code Playgroud) webrequest ×10
c# ×7
android ×2
.net ×1
asp.net ×1
bad-request ×1
cookies ×1
getresponse ×1
java ×1
javascript ×1
json ×1
parameters ×1
proxy ×1
request ×1
streamwriter ×1
winforms ×1
xamarin ×1