积极答复者
有谁用WebClient(Upload)或WebRequest(Post)成功调用过J2ee的Servlet吗?

问题
-
用Upload或POST两种方式调用时,Servlet端的doPost方法中没有任何反应。但用Download或GET方式时,servlet的doGet方法倒是可以被执行。什么原因呢?难道是人品问题O(∩_∩)O
=== 新添加内容 编辑者 小鱼o∩o @ 2008年10月10日 上午 1:34 UTC===
我在aspx中用同步方式UploadString就没问题了!可在silverlight中不支持同步调用呀?
Code SnippetString content = "a=a";
Uri endPoint = new Uri("http://localhost:8080/RESTProxyWeb/Test");
WebClient client = new WebClient();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
client.UploadStringAsync(endPoint, content);void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error == null)
{
Debug.WriteLine("upResult="+e.Result);
}
}Code SnippetString content = "a=a";
Uri endPoint = new Uri("http://localhost:8080/RESTProxyWeb/Test");WebRequest request = WebRequest.Create(endPoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);//RequestDelegate
private void RequestReady(IAsyncResult asyncResult)
{
WebRequest request = asyncResult.AsyncState as WebRequest;
Stream requestStream = request.EndGetRequestStream(asyncResult);
requestStream.Write(Encoding.UTF8.GetBytes(content),0,content.Length);
requestStream.Close();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}//ResponseDelegate
void ResponseReady(IAsyncResult asyncResult)
{
WebRequest request = asyncResult.AsyncState as WebRequest;
WebResponse response = request.EndGetResponse(asyncResult);using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
Debug.WriteLine("Result=" + new StreamReader(responseStream).ReadToEnd());}
}2008年10月9日 10:13
答案
-
问题解决了。原来是策略文件的问题。我的策略文件在line4中缺少http-request-headers="*"属性,导致GET可以而POST不行。faint。。。。。。。。。。。。。。。。。
Code Snippet1<access-policy>
2 <cross-domain-access>
3 <policy>
4 <allow-from http-request-headers="*">
5 <domain uri="*"/>
6 </allow-from>
7 <grant-to>
8 <resource path="/" include-subpaths="true"/>
9 </grant-to>
</policy>
</cross-domain-access>
</access-policy>2008年10月10日 4:01
全部回复
-
Code Snippet呵呵,我看错了,还以为是通用的.NET了
经过测试,是完全可以的,serverlet可以接收到数据。
但在返回到界面时,可能会遇到些问题。
完整测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace SilverlightApplication2
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
String content = "a=asdsdsd";
private void Button_Click(object sender, RoutedEventArgs e)
{
Uri endPoint = new Uri("http://xxxxx/admin", UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
private void RequestReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
StreamWriter sw = new StreamWriter(request.EndGetRequestStream(asyncResult));
//Stream requestStream = request.EndGetRequestStream(asyncResult);
//requestStream.Write(Encoding.UTF8.GetBytes(content), 0, content.Length);
//requestStream.Close();
sw.Write(content);
sw.Close();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}
//ResponseDelegate
void ResponseReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
content3.Text = new StreamReader(responseStream).ReadToEnd();
Debug.WriteLine("Result=" + new StreamReader(responseStream).ReadToEnd());
}
}
}
2008年10月10日 3:46 -
Code Snippet下面的代码搞定了UI的问题,居然还需要加 SynchronizationContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace SilverlightApplication2
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
syncContext = SynchronizationContext.Current;
}
SynchronizationContext syncContext;
String content = "a=asdsdsd";
private void Button_Click(object sender, RoutedEventArgs e)
{
Uri endPoint = new Uri("http://www.jitar.com.cn:8080/Groups/admin", UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
private void RequestReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
StreamWriter sw = new StreamWriter(request.EndGetRequestStream(asyncResult));
//Stream requestStream = request.EndGetRequestStream(asyncResult);
//requestStream.Write(Encoding.UTF8.GetBytes(content), 0, content.Length);
//requestStream.Close();
sw.Write(content);
sw.Close();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}
//ResponseDelegate
void ResponseReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse;
syncContext.Post(ExtractResponse, response);
}
private void ExtractResponse(object state)
{
HttpWebResponse response = state as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
content3.Text = new StreamReader(responseStream).ReadToEnd();
Debug.WriteLine("Result=" + new StreamReader(responseStream).ReadToEnd());
}
}
}
2008年10月10日 3:54 -
问题解决了。原来是策略文件的问题。我的策略文件在line4中缺少http-request-headers="*"属性,导致GET可以而POST不行。faint。。。。。。。。。。。。。。。。。
Code Snippet1<access-policy>
2 <cross-domain-access>
3 <policy>
4 <allow-from http-request-headers="*">
5 <domain uri="*"/>
6 </allow-from>
7 <grant-to>
8 <resource path="/" include-subpaths="true"/>
9 </grant-to>
</policy>
</cross-domain-access>
</access-policy>2008年10月10日 4:01