将java代码转换为c#的问题

时间:2015-11-17 08:43:57

标签: asp.net winforms

我试图将java代码转换为cSharp .I结构如何编码数据,所以请帮我怎么做..

我试图直接传递值而不进行编码,然后我得到错误

The remote server returned an error: (400) Bad Request. 

如果我删除了标题,那么错误就不会到来,我在网站上放置了断点,执行顺序没有在断点处停止

这是我的java代码

packagename=encodeData("some value");
username=encodeData("some value");
password=encodeData("some value");

hc= new DefaultHttpClient();
get= new HttpGet("http://techpalle.com/skillgun_App.svc/mobile/papers/"+chap_id+"/app");
get.setHeader(PaperActivity.this.getString(R.string.username),username);
get.setHeader(PaperActivity.this.getString(R.string.password),password);
get.setHeader(PaperActivity.this.getString(R.string.packagename),packagename);

转换后的csharp代码

string username=null;
string password=null;
string packagename=null;

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://localhost:2850/TechpalleNew/Skillgun_App.svc/mobile/chapters/" + "16" + "/app");
wr.Headers["username"] = "username";
wr.Headers["password"] = "password";
wr.Headers["packagename"] = "packagename";
WebResponse resp = wr.GetResponse();
StreamReader read = new StreamReader(resp.GetResponseStream());
string res = read.ReadToEnd();

我的网站代码

namespace ISkillgun_App
{
  [ServiceContract]
 public interface ISkillgun_App
 {
    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "mobile/chapters/{sub_topic_id}/app")]
    List<ChapterNames> Chapters_Names(string sub_topic_id);

    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "mobile/papers/{chapter_id}/app")]
    List<Paper_Ids> Paper_ids(string chapter_id);

    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "mobile/questions/{paper_id}/app")]
    List<Qtions_data> Qtions_data(string paper_id);        
 }
}

namespace Skillgun_App:ISkillgun_App
{
 public List<ChapterNames> Chapters_Names(string sub_topic_id)
 Breakppoint    {
        List<ChapterNames> chptrsdetails = new List<ChapterNames>();
        using (SqlConnection cn = new SqlConnection(strConnection))
        {
            using (SqlCommand cmd = new SqlCommand("skillgun_mobile_app_Qtions_data", cn))
            {                    
                cmd.CommandType = CommandType.StoredProcedure;                    
                try
                {
                    SqlParameter p1 = new SqlParameter("@type", SqlDbType.VarChar, 20);
                    SqlParameter p2 = new SqlParameter("@topic_or_paper_id", SqlDbType.Int);
                    p1.Value = "chapters_data";
                    p2.Value = sub_topic_id;
                    cmd.Parameters.Add(p1);
                    cmd.Parameters.Add(p2);

                    cmd.Parameters.Add("@username", SqlDbType.VarChar, 40);
                    cmd.Parameters["@username"].Direction = ParameterDirection.Output;

                    cmd.Parameters.Add("@password", SqlDbType.VarChar, 40);
                    cmd.Parameters["@password"].Direction = ParameterDirection.Output;

                    cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 50);
                    cmd.Parameters["@package_name"].Direction = ParameterDirection.Output;

                    cn.Open();
                    SqlDataReader drtopics = cmd.ExecuteReader();
                    while (drtopics.Read())
                    {
                        ChapterNames qbsubchapter = new ChapterNames();
                        qbsubchapter.Qbc_id = (int)drtopics["qbc_id"];
                        qbsubchapter.Qbc_nameofChapter = drtopics["qbc_nameofChapter"].ToString();
                        qbsubchapter.Qbc_no_of_papers = (int)drtopics["qbc_no_of_papers"];
                        qbsubchapter.Qbc_no_of_questions = (int)drtopics["qbc_no_of_questions"];

                        chptrsdetails.Add(qbsubchapter);
                    }
                    drtopics.Close();

                    cn.Close();

                    string topic_username = cmd.Parameters["@username"].Value.ToString();
                    string topic_password = cmd.Parameters["@password"].Value.ToString();
                    string topic_package_name = cmd.Parameters["@package_name"].Value.ToString();                        
                    bool result = check_user(topic_username, topic_password, topic_package_name);

                    if (result == true)                                              
                        return chptrsdetails;                        
                    else
                        return null;
                }
                catch (SqlException ex)
                {
                    return null;
                }
                finally
                {
                    if (cn.State == ConnectionState.Open)
                    {
                        cn.Close();
                    }
                }
            }
        }
    }

  public bool check_user(string topic_username, string topic_password, string topic_package_name)
    {
        string requestedUrl = OperationContext.Current.IncomingMessageHeaders.To.AbsoluteUri;
        if (WebOperationContext.Current != null)
        {
            if (WebOperationContext.Current.IncomingRequest != null)
            {
                if (WebOperationContext.Current.IncomingRequest.Headers["username"] != null && WebOperationContext.Current.IncomingRequest.Headers["password"] != null && WebOperationContext.Current.IncomingRequest.Headers["packagename"] != null)
                {
                    string username = WebOperationContext.Current.IncomingRequest.Headers["username"];
                    string pwd = WebOperationContext.Current.IncomingRequest.Headers["password"];
                    string pckgname = WebOperationContext.Current.IncomingRequest.Headers["packagename"];
                    bool result = Base64Decode(username, pwd, pckgname, topic_username, topic_password, topic_package_name);
                    if (result == true)
                        return true;
                    else
                        return false;
                }
            }
        }
        return false;
    }

1 个答案:

答案 0 :(得分:0)

对于html编码(您没有指定其他编码),请使用静态方法

HttpUtility.HtmlEncode(string)

Documentation (near bottom)